【问题标题】:Trying to perform sum of count using group by and union尝试使用 group by 和 union 执行计数总和
【发布时间】:2012-07-10 03:44:14
【问题描述】:

我正在尝试使用联合计算三个查询的计数。这些查询还包含其中的 group by 子句。以下是我写的查询:

select 
       extract(year from start_date), 
       extract(month from start_date),
       APPLICATION_TYPE,
       sum(TOTAL) as Overall_TOTAL
from (
select 
       extract(year from A.start_date) as Start_Year, 
       extract(month from A.start_date) as Start_Month,
       A.APPLICATION_TYPE as Type,
       count(A.TRANSACTION_NUMBER) as Total
from lnr_application A
where 
      A.START_DATE >= to_date('&sdate','DD/MM/YYYY')
      and A.START_DATE <= to_date('&edate','DD/MM/YYYY')  
      and A.permission_type = 'HRW'
      and A.status_cd in ('AP')
group by extract(year from start_date), extract(month from start_date), A.APPLICATION_TYPE
union all
select 
       extract (year from A.tstamp) as Start_Year, 
       extract (month from A.tstamp) as Start_Month, 
       A.application_type as Type,
       count(A.transaction_number) as Total
from lnr_application A
where 
      A.permission_type = 'HRW'
      and A.status_cd in ('RF')
      and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
      and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')
group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type 
union all
select 
       extract (year from A.tstamp) as Start_Year, 
       extract (month from A.tstamp) as Start_Month, 
       A.application_type as Type, 
       count(A.transaction_number) as Total
from lnr_application A
where 
      A.permission_type = 'HRW'
      and A.status_cd in ('CL')
      and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
      and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')      
group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type
) tmp
group by extract(year from start_date), extract(month from start_date), APPLICATION_TYPE
order by extract(year from start_date), extract(month from start_date), APPLICATION_TYPE

当我执行查询时,我收到一条错误消息,指出 Start_Date 是无效标识符。如果我从顶部删除 sum 组件,即合并所有三个查询,我会得到以下结果:

2011    7   A   627 
2011    7   A   21 
2011    7   A   1 
2011    7   C   1585 
2011    7   C   1    
2011    7   I   1 
2011    7   I   154 
2011    7   I   3

我想合计各个年份、月份和申请类型的计数总和,如下所示:

2011    7   A   649
2011    7   C   1586 
2011    7   I   158

有人可以帮帮我吗?

【问题讨论】:

    标签: sql oracle count group-by union


    【解决方案1】:

    在 union 的 SELECT 语句中,需要使用 Start_Year 和 Start_Month 而不是 EXTRACT 语句。此外,使用 Type 而不是 Application_Type。

    【讨论】:

    • @bdamania,很高兴听到。由于您是新手,最好将答案标记为“已回答”,以便其他人可以看到它是适合您的解决方案。您可以通过单击答案旁边的复选标记(在 upvote/downvote 按钮下)来执行此操作。无论声誉如何,您都可以这样做。
    【解决方案2】:

    您可以将三个联合所有子查询合并为一个查询,然后您不需要额外的内联视图,导致这个简单的查询:

    select extract(year from a.start_date)
         , extract(month from a.start_date)
         , a.application_type
         , sum(a.total) as overall_total
      from lnr_application a
     where a.permission_type = 'HRW'
       and a.status_cd in ('AP','RF','CL')
       and a.tstamp between to_date('&sdate','dd/mm/yyyy') and to_date('&edate','dd/mm/yyyy') + interval '1' day - interval '1' second
     group by extract(year from a.start_date)
         , extract(month from a.start_date)
         , a.application_type
     order by extract(year from a.start_date)
         , extract(month from a.start_date)
         , a.application_type
    

    并通过使用 trunc(...,'mm') 而不是两次提取来进一步简化:

    select extract(year from trunc(a.start_date,'mm'))
         , extract(month from trunc(a.start_date,'mm'))
         , a.application_type
         , sum(a.total) as overall_total
      from lnr_application a
     where a.permission_type = 'HRW'
       and a.status_cd in ('AP','RF','CL')
       and a.tstamp between to_date('&sdate','dd/mm/yyyy') and to_date('&sdate','dd/mm/yyyy') + interval '1' day - interval '1' second
     group by trunc(a.start_date,'mm')
         , a.application_type
     order by trunc(a.start_date,'mm')
         , a.application_type
    

    问候,
    抢。

    【讨论】:

    • 您好 Rob,非常感谢您的回复。我猜测简化对我不起作用,因为第一个子查询对 Start_DATE 有一个约束,状态为 AP,而另外两个 STATUS 为 CL 和 RF,约束在 TSTAMP。因此,猜测它需要分开。但是,将 extract 语句更改为别名,对我来说很有效。
    • 啊,我错过了。不过,您仍然可以发送( (status_cd = 'AP' and a.start_date between ...) or (status_cd in ('RF','CL') and (a.tstamp between ...) )
    【解决方案3】:
    select 
           Start_Year, 
           Start_Month,
           APPLICATION_TYPE,
           sum(TOTAL) as Overall_TOTAL
    from (
    select 
           extract(year from A.start_date) as Start_Year, 
           extract(month from A.start_date) as Start_Month,
           A.APPLICATION_TYPE as Type,
           count(A.TRANSACTION_NUMBER) as Total
    from lnr_application A
    where 
          A.START_DATE >= to_date('&sdate','DD/MM/YYYY')
          and A.START_DATE <= to_date('&edate','DD/MM/YYYY')  
          and A.permission_type = 'HRW'
          and A.status_cd in ('AP')
    group by extract(year from start_date), extract(month from start_date), A.APPLICATION_TYPE
    union all
    select 
           extract (year from A.tstamp) as Start_Year, 
           extract (month from A.tstamp) as Start_Month, 
           A.application_type as Type,
           count(A.transaction_number) as Total
    from lnr_application A
    where 
          A.permission_type = 'HRW'
          and A.status_cd in ('RF')
          and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
          and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')
    group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type 
    union all
    select 
           extract (year from A.tstamp) as Start_Year, 
           extract (month from A.tstamp) as Start_Month, 
           A.application_type as Type, 
           count(A.transaction_number) as Total
    from lnr_application A
    where 
          A.permission_type = 'HRW'
          and A.status_cd in ('CL')
          and trunc(A.tstamp) >= to_date ('&sdate','dd/mm/yyyy')
          and trunc(A.tstamp) <= to_date ('&edate','dd/mm/yy')      
    group by extract (year from A.tstamp), extract (month from A.tstamp), A.application_type
    ) tmp
    group by Start_Year, 
           Start_Month,
           APPLICATION_TYPE
    order by Start_Year, 
           Start_Month,
           APPLICATION_TYPE
    

    【讨论】:

    • 谢谢弗洛林,霍尔格指出了同样的事情,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多