SQL获取总数并同时使用pivot

Posted

技术标签:

【中文标题】SQL获取总数并同时使用pivot【英文标题】:SQL get total number and using pivot at the same time 【发布时间】:2020-06-06 11:07:47 【问题描述】:

我有两个表的关系是这样的

我有一个这样的数据集

并希望获得所有过失、代码、描述、迄今为止在任何一个月(任何一年)中针对过失代码犯下的罪行总数,然后是为过失犯下的罪行总数 每个月(任何一年)的代码基本上是这样的结果

我可以分别获取总数(通过使用分组依据)和每月数据(通过使用透视行作为列),但不知道如何在一个查询中获取它们。任何帮助将不胜感激。

 select * from (
 select 
        d.dem_code, 
        d.dem_code as dem_code_copy,
        d.dem_description, 
        case 
        when EXTRACT(month FROM off_datetime) = 1 then 'Jan'
        when EXTRACT(month FROM off_datetime) = 2 then 'Feb'
        when EXTRACT(month FROM off_datetime) = 3 then 'Mar'
        when EXTRACT(month FROM off_datetime) = 4 then 'Apr'
        when EXTRACT(month FROM off_datetime) = 5 then 'May'
        when EXTRACT(month FROM off_datetime) = 6 then 'Jun'
        when EXTRACT(month FROM off_datetime) = 7 then 'Jul'
        when EXTRACT(month FROM off_datetime) = 8 then 'Aug'
        when EXTRACT(month FROM off_datetime) = 9 then 'Sep'
        when EXTRACT(month FROM off_datetime) = 10 then 'Oct'
        when EXTRACT(month FROM off_datetime) = 11 then 'Nov'
        when EXTRACT(month FROM off_datetime) = 12 then 'Dec'
        end
        as "Month"    
    from demerit d
    left join offence o on d.dem_code = o.dem_code
    order by d.dem_code 
    )
    pivot(
    count(dem_code_copy)
    for "Month"
    in (
        'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'
    )
)

这个查询给了我每月的结果


    select 
        d.dem_code, 
        d.dem_description,
        count(o.off_no) as total
    from demerit d
    left join offence o on d.dem_code = o.dem_code
    group by d.dem_code, d.dem_description
    order by d.dem_code 

这个查询给出总数

【问题讨论】:

【参考方案1】:

我只会使用条件聚合:

select d.dem_code, d.dem_description, 
       sum(case when EXTRACT(month FROM off_datetime) = 1 then 1 else 0 end) as Jan,
       sum(case when EXTRACT(month FROM off_datetime) = 2 then 1 else 0 end) as Feb
       sum(case when EXTRACT(month FROM off_datetime) = 3 then 1 else 0 end) as Mar,
       sum(case when EXTRACT(month FROM off_datetime) = 4 then 1 else 0 end) as Apr,
       sum(case when EXTRACT(month FROM off_datetime) = 5 then 1 else 0 end) as May,
       sum(case when EXTRACT(month FROM off_datetime) = 6 then 1 else 0 end) as Jun,
       sum(case when EXTRACT(month FROM off_datetime) = 7 then 1 else 0 end) as Jul,
       sum(case when EXTRACT(month FROM off_datetime) = 8 then 1 else 0 end) as Aug,
       sum(case when EXTRACT(month FROM off_datetime) = 9 then 1 else 0 end) as Sep,
       sum(case when EXTRACT(month FROM off_datetime) = 10 then 1 else 0 end) as Oct,
       sum(case when EXTRACT(month FROM off_datetime) = 11 then 1 else 0 end) as Nov,
       sum(case when EXTRACT(month FROM off_datetime) = 12 then 1 else 0 end) as Dec
from demerit d left join
     offence o
     on d.dem_code = o.dem_code
group by d.dem_code, d.dem_description;

【讨论】:

这正是我想要的。非常感谢。

以上是关于SQL获取总数并同时使用pivot的主要内容,如果未能解决你的问题,请参考以下文章

获取 ROWS 作为 COLUMNS(SQL Server 动态 PIVOT 查询)

SQL pivot 按 orderNr 获取产品

Oracle SQL Pivot -- 获取行总计

SQL获取每日注册用户总数

如何从两个表中获取总数并使用该值进行计算? (sql, asp)

至少对于获取的批量数据,Spark-sql Pivoting 无法按预期工作