我正在使用 Oracle 数据库,我无法从 2 个结果集中获得一个结果。
我有桌子案例
Create table cases (ID varchar(1), date_entered date, sub_category varchar (5));
insert into cases (id, date_entered, sub_category)
values('1', to_date('2012/05/03','yyyy/mm/dd'),'Temp1');
insert into cases (id, date_entered, sub_category)
values('2', to_date('2012/06/01','yyyy/mm/dd'),'Temp2');
insert into cases (id, date_entered, sub_category)
values('3', to_date('2012/03/15','yyyy/mm/dd'),'Temp3');
insert into cases (id, date_entered, sub_category)
values('4', to_date('2012/03/01','yyyy/mm/dd'),'Call1');
insert into cases (id, date_entered, sub_category)
values('5', to_date('2012/03/08','yyyy/mm/dd'),'Call2');
insert into cases (id, date_entered, sub_category)
values('6', to_date('2012/02/20','yyyy/mm/dd'),'Call2');
并且需要按SUB CATEGORIES,BY MONTH统计记录,其中一个计数包括sub_category:Temp1,Temp2,Temp3其他计数包括sub_category:Call1,Call2,Call3
我做了查询1:
With skills
AS
(
Select sub_category,
date_entered,
extract(MONTH FROM cases.date_entered) as month_entered,
count (*)
from cases
where
SUB_CATEGORY IN('Temp1', 'Temp2', 'Temp3')
group by cases.sub_category, cases.date_entered
order by to_char(cases.date_entered,'MM')
)
select s.month_entered,
count(*)as skill_count
from skills s
group by s.month_entered
ORDER BY CAST(s.month_entered AS INTEGER) ASC
结果:
MONTH_ENTERED SKILL_COUNT
3 1
5 1
6 1
和查询2:
With training
AS
(
Select sub_category,
date_entered,
extract(MONTH FROM cases.date_entered) as month_entered,
count (*)
from cases
where
SUB_CATEGORY IN('Call1', 'Call2', 'Call3')
group by cases.sub_category, cases.date_entered
order by to_char(cases.date_entered,'MM')
)
select t.month_entered,
count(*)as training_count
from training t
group by t.month_entered
ORDER BY CAST(t.month_entered AS INTEGER) ASC
结果:
MONTH_ENTERED TRAINING_COUNT
2 1
3 2
我需要从这两个查询中得到的结果是:
MONTH_ENTERED SKILL_COUNT TRAINING_COUNT
2 0 1
3 1 2
5 1 0
6 1 0
尝试联合并离开加入,但没有给我这个结果。
这是 sqlfiddle 示例http://sqlfiddle.com/#!4/504cd/31。