postgresql - 如何按各组总计对ROLLUP结果进行排序

标签 postgresql rollup

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

    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 <<<

我遇到的问题是我想对结果重新排序,使总计最低的组首先出现,这样结果将如下所示:

    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 BY actname, ROLLUP(year)

将同一 Actor 名称和年份的所有门票金额合并在一起。

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

谢谢

最佳答案

使用窗口函数(在本例中为 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

关于postgresql - 如何按各组总计对ROLLUP结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70009672/

相关文章:

postgresql - 外部数据包装并发请求

sql - Postgres - 按 session 聚合用户事件

postgresql - 对于与 parent 的帖子,我应该使用哪种嵌套数据结构?

vue.js - Vue SFC 中的 SCSS 别名通过 Rollup

javascript - 我应该如何测试不同的汇总包

postgresql - 创建新的架构、表并在同一 EXECUTE 中插入数据

postgresql - 如何将另一列添加到 PostgreSQL 子查询?

sql - 我想将 ROLLUP 与 PIVOT 结合使用 - 这是一个选项吗?

javascript - 无法在带有 webpack/rollup/react 的外部库中使用 Material UI

带有 ROLLUP 和 LIMIT 的 MYSQL 聚合