I am using Oracle database and I’m having problem to get one result from 2 result sets.
I have table CASES
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');
and need to count records BY SUB CATEGORIES, BY MONTH, where one count includes sub_category: Temp1, Temp2, Temp3 other count includes sub_category: Call1, Call2, Call3
I have made query1:
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
with result:
MONTH_ENTERED SKILL_COUNT
3 1
5 1
6 1
and query 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
with result:
MONTH_ENTERED TRAINING_COUNT
2 1
3 2
The result that I need from these 2 queries is:
MONTH_ENTERED SKILL_COUNT TRAINING_COUNT
2 0 1
3 1 2
5 1 0
6 1 0
Tried union and left join but nothing gives me this result.
Here is sqlfiddle example http://sqlfiddle.com/#!4/504cd/31.
1 Answer