Oracle SQL:如何使用所有组的总和对组求和

ygya80vv  于 2023-03-22  发布在  Oracle
关注(0)|答案(1)|浏览(219)

我想做的基本上是制造一个Sum of sums,只有在枢转时才真正有意义。
我试图分析总的系统降级,但也将其视为DownDegraded作为单独的调用-这就是我遇到麻烦的地方。此外,如果给定时段没有值,则假定为0。我试图创建一个虚构的sum(a) + sum(b)列,但我不知道如何执行此操作
下面是数据库中的数据,当对每个类别应用sum时(我显式地引入了一个年份跳转,impact的null值被归类为'TBD'):
所以,按周总结如下:
| 年|周|影响|持续时间_分钟|
| - ------|- ------|- ------|- ------|
| 小行星2022|五十|2 -降解|一百七十五|
| 小行星2022|五十一|1 -向下|七十|
| 小行星2022|五十一|2 -降解|五二二|
| 二○二三|1个|1 -向下|四百六十|
| 二○二三|1个|2 -降解|小行星1258|
| 二○二三|第二章|1 -向下|五十八|
| 二○二三|第二章|2 -降解|九十八|
但是,这就是我的目标:
| 年|周|0 -待定|2 -降解|1 -向下|共计|
| - ------|- ------|- ------|- ------|- ------|- ------|
| 小行星2022|五十|无|一百七十五|无|一百七十五|
| 小行星2022|五十一|无|五二二|七十|五九二|
| 二○二三|1个|无|小行星1258|四百六十|小行星一七一八|
| 二○二三|第二章|无|九十八|五十八|一百五十六|
样本数据:

CREATE TABLE MY_TABLE
(
    cy               INT          NOT NULL,
    week             INT          NOT NULL,
    IMPACT           VARCHAR2(12) NOT NULL,
    duration_minutes NUMBER       NOT NULL
);

INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 50, '2 - Degraded', 42);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 50, '2 - Degraded', 88);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 50, '2 - Degraded', 45);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 51, '1 - Down', 70);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 51, '2 - Degraded', 86);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 51, '2 - Degraded', 220);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2022, 51, '2 - Degraded', 216);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '1 - Down', 29);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '1 - Down', 62);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '1 - Down', 369);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '2 - Degraded', 58);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '2 - Degraded', 42);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '2 - Degraded', 277);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 1, '2 - Degraded', 881);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 2, '1 - Down', 40);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 2, '1 - Down', 18);
INSERT INTO my_table(CY, WEEK, IMPACT, DURATION_MINUTES) VALUES (2023, 2, '2 - Degraded', 98);

commit;

下面是生成未透视数据的选择:

SELECT cy,
       week,
       impact,
       SUM(duration_minutes) AS duration_minutes
  FROM my_table
 GROUP BY cy, week, impact
 ORDER BY cy, week, impact;
vaj7vani

vaj7vani1#

从GROUP BY列表中删除列impact时,可以使用条件求和聚合,例如

SELECT cy, week, 
       SUM(CASE WHEN impact IS NULL THEN duration_minutes ELSE 0 END) AS "0 - TBD",
       SUM(CASE WHEN impact = '2 - Degraded' THEN duration_minutes ELSE 0 END) AS "2 - Degraded",
       SUM(CASE WHEN impact = '1 - Down' THEN duration_minutes ELSE 0 END) AS "1 - Down",
       SUM(duration_minutes) AS duration_minutes
  FROM my_table
 GROUP BY cy, week
 ORDER BY 1, 2

Demo

相关问题