0

我正在使用 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

4

2 回答 2

2

我认为您可以将查询大大简化为:

      Select extract(MONTH FROM cases.date_entered) as month_entered, 
             sum(case when SUB_CATEGORY IN('Temp1', 'Temp2', 'Temp3') then 1 else 0 end) as Skill_Count,
             sum(case when SUB_CATEGORY IN('Call1', 'Call2', 'Call3') then 1 else 0 end) as Skill_Count,
      from cases 
      group by extract(MONTH FROM cases.date_entered)
      order by extract(MONTH FROM cases.date_entered)
于 2012-11-20T15:20:07.433 回答
2
SELECT
  EXTRACT(MONTH FROM CASES.DATE_ENTERED) AS MONTH_ENTERED,
  COUNT (CASE WHEN SUB_CATEGORY IN('Temp1', 'Temp2', 'Temp3') THEN 1 END) SKILL_COUNT,
  COUNT (CASE WHEN SUB_CATEGORY IN('Call1', 'Call2', 'Call3') THEN 1 END) TRAINING_COUNT
FROM CASES
GROUP BY EXTRACT(MONTH FROM DATE_ENTERED)
ORDER BY MONTH_ENTERED ASC
于 2012-11-20T15:22:06.930 回答