【问题标题】:How to order the result of ROLLUP by each groups total如何按每组总计对 ROLLUP 的结果进行排序
【发布时间】:2021-12-28 17:48:55
【问题描述】:

所以我有以下查询产生以下结果:

    actname    | year     | tickets
---------------+----------+---------
 Join Division | 2016     |       2
 Join Division | 2018     |       2
 Join Division | 2020     |       3
 Join Division | Total    |       7 <<<
 QLS           | 2018     |       2
 QLS           | 2019     |       1
 QLS           | Total    |       3 <<<
 Scalar Swift  | 2017     |       3
 Scalar Swift  | 2018     |       1
 Scalar Swift  | 2019     |       1
 Scalar Swift  | Total    |       5 <<<
 The Selecter  | 2017     |       4
 The Selecter  | 2018     |       4
 The Selecter  | Total    |       8 <<<
 The Where     | 2016     |       1
 The Where     | 2017     |       3
 The Where     | 2018     |       5
 The Where     | 2020     |       4
 The Where     | Total    |      13 <<<
 ViewBee 40    | 2017     |       3
 ViewBee 40    | 2018     |       1
 ViewBee 40    | Total    |       4 <<<

我遇到的问题是我想重新排序结果,以便首先出现 Total 最低的组,结果如下所示:

    actname    | year     | tickets
---------------+----------+---------
 QLS           | 2018     |       2
 QLS           | 2019     |       1
 QLS           | Total    |       3 <<<
 ViewBee 40    | 2017     |       3
 ViewBee 40    | 2018     |       1
 ViewBee 40    | Total    |       4 <<<
 Scalar Swift  | 2017     |       3
 Scalar Swift  | 2018     |       1
 Scalar Swift  | 2019     |       1
 Scalar Swift  | Total    |       5 <<<
 Join Division | 2016     |       2
 Join Division | 2018     |       2
 Join Division | 2020     |       3
 Join Division | Total    |       7 <<<
 The Selecter  | 2017     |       4
 The Selecter  | 2018     |       4
 The Selecter  | Total    |       8 <<<
 The Where     | 2016     |       1
 The Where     | 2017     |       3
 The Where     | 2018     |       5
 The Where     | 2020     |       4
 The Where     | Total    |      13 <<<

我正在使用以下 GROUP 获得结果:

GROUP BY actname, ROLLUP(year)

这就是将同一幕名和年份的所有门票金额组合在一起。

如有必要,我可以提供完整的查询!

谢谢

【问题讨论】:

    标签: postgresql rollup


    【解决方案1】:

    使用窗口函数(在这种情况下为sum()),您可以为组设置值(组由actname 列分区),所以现在actname 列中的每个组都具有相同的值,就像它自己的一样year='Total' 所在的行。

    然后简单地按新列排序,如下所示:

    with t(actname, year, tickets) as (
    VALUES
    ('Join Division','2016',2),
    ('Join Division','2018',2),
    ('Join Division','2020',3),
    ('Join Division','Total',7),
    ('QLS','2018',2),
    ('QLS','2019',1),
    ('QLS','Total',3 ),
    ('Scalar Swift','2017',3),
    ('Scalar Swift','2018',1),
    ('Scalar Swift','2019',1),
    ('Scalar Swift','Total',5 ),
    ('The Selecter','2017',4),
    ('The Selecter','2018',4),
    ('The Selecter','Total',8 ),
    ('The Where','2016',1),
    ('The Where','2017',3),
    ('The Where','2018',5),
    ('The Where','2020',4),
    ('The Where','Total',13 ),
    ('ViewBee 40','2017',3),
    ('ViewBee 40','2018',1),
    ('ViewBee 40','Total',4 )
    )
    SELECT * FROM (
        select *,  sum(case when year = 'Total' then tickets end) over(partition by actname) sm from t
    ) tt
    ORDER BY sm, year
    

    【讨论】:

    • 这很棒!谢谢你。您能否详细解释一下 SUM 部分的工作原理?
    • @NoahHollowell - 如果有帮助,编辑并在答案中添加一点解释,那就太好了。
    • 解释很好。谢谢!
    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 2018-07-25
    • 2012-09-19
    • 2016-01-06
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多