如何计算每月在oracle中返回的行数?

时间:2019-01-14 07:41:00

标签: sql oracle pivot

我正在为我的应用程序创建功能,我需要在整个2018年生成报告。

我需要计算2018年的所有门票。每张门票都有一个类别。 例如: 改名。 老年人等

我需要计算每月2018年的更名机票,老年人​​机票

我试图做到这一点,但似乎无法获得想要的结果。

我似乎无法细分每月的计数。

这是我到目前为止的查询:

SELECT SUBCATEGORY,COUNT(ticket_no) FROM CNR_TICKET WHERE date_created >= to_date('1/01/2018','MM/DD/YYYY') and date_created <= to_date('12/31/2018','MM/DD/YYYY') GROUP BY SUBCATEGORY;

这是我要查看的列:

CATEGORY | JAN | FEB | MARCH | APRIL | MAY | JNE | JUL | AUG | SEPT | OCT| NOV| DEC
SENIOR      2     5      20      50     1     11    23    4      1     2    4    6
COAN        23   55      22      55     6      2    12    23    12     12   5   89

3 个答案:

答案 0 :(得分:1)

类似这样的东西:

SELECT
    SUBCATEGORY,    
    count( distinct case when EXTRACT(month FROM date_created) = 1  then ticket_no else null end) as JAN,
    count( distinct case when EXTRACT(month FROM date_created) = 2  then ticket_no else null end) as FEB,
    count( distinct case when EXTRACT(month FROM date_created) = 3  then ticket_no else null end) as MARCH,
    count( distinct case when EXTRACT(month FROM date_created) = 4  then ticket_no else null end) as APRIL,
    count( distinct case when EXTRACT(month FROM date_created) = 5  then ticket_no else null end) as MAY,
    count( distinct case when EXTRACT(month FROM date_created) = 6  then ticket_no else null end) as JNE,
    count( distinct case when EXTRACT(month FROM date_created) = 7  then ticket_no else null end) as JUL,
    count( distinct case when EXTRACT(month FROM date_created) = 8  then ticket_no else null end) as AUG,
    count( distinct case when EXTRACT(month FROM date_created) = 9  then ticket_no else null end) as SEPT,
    count( distinct case when EXTRACT(month FROM date_created) = 10 then ticket_no else null end) as OCT,
    count( distinct case when EXTRACT(month FROM date_created) = 11 then ticket_no else null end) as NOV,
    count( distinct case when EXTRACT(month FROM date_created) = 12 then ticket_no else null end) as DEC
FROM
    CNR_TICKET
WHERE
    date_created >= to_date('1/01/2018','MM/DD/YYYY') and
    date_created <= to_date('12/31/2018','MM/DD/YYYY')
GROUP BY
    SUBCATEGORY

您可以使用来更改WHERE子句:

EXTRACT(year FROM date_created ) = 2018

答案 1 :(得分:1)

您可以尝试PIVOT声明

select * from (
    select SUBCATEGORY, month(date_created) mon
    from CNR_TICKET
    where date_created >= to_date('1/01/2018','MM/DD/YYYY') and date_created <= to_date('12/31/2018','MM/DD/YYYY')
)
pivot ( 
    count(*) 
    for mon
    in ( 1 Jan, 2 Feb, 3 MARCH, 4 APRIL, 5 MAY, 6 JNE, 7 JUL, 8 AUG, 9 SEPT, 10 OCT, 11 NOV, 12 DEC )
)

答案 2 :(得分:1)

您可以通过以下方式使用Pivot关键字:将for month用作透视查询

  select *
    from
    ( 
    select subcategory, to_char(date_created,'mm') as month
      from cnr_ticket 
     where to_char(date_created,'yyyy')='2018'
      ) 
     pivot(
           count(*)
           for (month)
            in ('01' as jan ,'02' as feb, '03' as mar,
                '04' as apr ,'05' as may, '06' as jun,
                '07' as jul ,'08' as aug, '09' as sep,
                '10' as oct ,'11' as nov, '12' as dec
               )
       )

或使用conditional aggregation

    select subcategory,
           sum(case when to_char(date_created,'mm') = '01' then 1 else 0 end) as jan,
           sum(case when to_char(date_created,'mm') = '02' then 1 else 0 end) as feb,
           sum(case when to_char(date_created,'mm') = '03' then 1 else 0 end) as mar,
           sum(case when to_char(date_created,'mm') = '04' then 1 else 0 end) as apr,       
           sum(case when to_char(date_created,'mm') = '05' then 1 else 0 end) as may,
           sum(case when to_char(date_created,'mm') = '06' then 1 else 0 end) as jun,
           sum(case when to_char(date_created,'mm') = '07' then 1 else 0 end) as jul,
           sum(case when to_char(date_created,'mm') = '08' then 1 else 0 end) as aug,       
           sum(case when to_char(date_created,'mm') = '09' then 1 else 0 end) as sep,
           sum(case when to_char(date_created,'mm') = '10' then 1 else 0 end) as oct,
           sum(case when to_char(date_created,'mm') = '11' then 1 else 0 end) as nov,
           sum(case when to_char(date_created,'mm') = '12' then 1 else 0 end) as dec              
      from cnr_ticket
     where to_char(date_created,'yyyy')='2018' 
     group by subcategory  

Rextester Demo