【问题标题】:SQL pivot table total per columnSQL 数据透视表每列总计
【发布时间】:2019-02-27 09:32:15
【问题描述】:

所以我有这个有动态标题的表。

下面的查询用于生成将用于表查询的日期。

select listagg(INSERT_DATE,
''',''') WITHIN GROUP(ORDER BY INSERT_DATE)
from (select distinct INSERT_DATE from TEST_TBL order by INSERT_DATE asc)

上面查询的结果用于下面的 in 子句。

select * from (select log, lot, insert_date from TEST_TBL)
pivot(count(distinct log || insert_date)
for(insert_date) in ('17-JAN-19', '21-JAN-19', '22-JAN-19'))

现在,我希望得到这样的结果,每列的总数将显示在所有行的末尾。我尝试使用 GROUP BY ROLLUP 但它不起作用。

我试过这个查询:

    select * 
    from (select * from (select log, lot, insert_date from TEST_TBL) pivot(count(distinct log || insert_date)
    for(insert_date) in ('17-JAN-19', '21-JAN-19', '22-JAN-19')))
    group by rollup (log); 

有人可以帮我解决我应该使用什么查询吗?谢谢。

编辑:我使用了@q4za4 的解决方案,并得出了这个最终查询

from   (select *
            from   (select log,
                           lot,
                           insert_date
                    from   TEST_TBL)
            pivot(
                    count(distinct lot || insert_date)
                    for   (insert_date) 
                    in    ('17-JAN-19','21-JAN-19','22-JAN-19'))
    )
    UNION ALL
    select 'TOTAL # OF LOGS',
           sum(jan1719),sum(jan2119),sum(jan2219)
    FROM   (select *
            from   (select log,
                           lot,
                           insert_date
                    from   TEST_TBL)
            pivot(
                    count(distinct lot || insert_date)
                    for   (insert_date) 
                    in    (
                    '17-JAN-19' as jan1719,'21-JAN-19' as jan2119,'22-JAN-19' as jan2219
                    )))

也感谢@Ponder Stibbons 的解决方案,它也帮助了我,让我想到在我的 listagg-query 中添加附加部分来创建目标查询的第一行。

【问题讨论】:

    标签: sql oracle pivot rollup dynamic-columns


    【解决方案1】:

    最快的方法是复制相同的查询。比如:

    select *
    from   (select log,
                   lot,
                   insert_date
            from   TEST_TBL)
    pivot(
            count(distinct log || insert_date)
            for   (insert_date) 
            in    ('17-JAN-19', '21-JAN-19', '22-JAN-19'))
    )
    UNION ALL
    select NULL,
           sum(),
           sum(),
           sum(),
    FROM   (
            select *
            from   (select log,
                           lot,
                           insert_date
                    from   TEST_TBL)
            pivot(
                    count(distinct log || insert_date)
                    for   (insert_date) 
                    in    ('17-JAN-19', '21-JAN-19', '22-JAN-19'))
           )
    

    在 sum() 中的括号之间添加你的列名

    【讨论】:

    • 感谢您的回复!该解决方案非常适合我的应用程序,因为我需要在总数下方附加更多行。再次感谢!
    【解决方案2】:

    此查询有效:

    select lot, sum(d17) sd17, sum(d22) sd22, sum(d23) sd23
      from test_tbl
      pivot(count(distinct log) for(insert_date) in (date '2019-01-17' d17, 
                                                     date '2019-01-22' d22, 
                                                     date '2019-01-23' d23))
      group by rollup(lot)
    

    dbfiddle demo

    因此,如果您动态生成日期,您还必须在 listagg-query 中添加其他部分来创建目标查询的第一行,就像您为 in 子句所做的那样。您可以根据需要为列设置别名,然后尝试使用dbms_output 构建动态查询并检查语法是否正确。

    【讨论】:

    • 感谢您的回复!但我将不得不接受上述答案,因为联合解决方案非常适合我的应用程序,因为我需要在下面附加更多行。但是,如果您只需要列的总数,这是一个非常有用的解决方案。
    猜你喜欢
    • 2012-03-24
    • 2023-04-04
    • 2019-03-31
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多