Oracle SQL - 使用 select 为某些行生成聚合行

Oracle SQL - Generate aggregate rows for certain rows using select

我有一个 table 如下所示。

|FILE| ID |PARENTID|SHOWCHILD|CAT1|CAT2|CAT3|TOTAL|
|F1  | A1 |  P1    |     N   | 3  | 2  | 6  | 11  |
|F2  | A2 |  P2    |     N   | 4  | 7  | 3  | 14  |
|F3  | A3 |  P1    |     N   | 3  | 1  | 1  | 5   |
|F4  | LG1|        |     Y   | 6  | 3  | 7  | 16  |
|F5  | LG2|        |     Y   | 4  | 7  | 3  | 14  |

现在,如果我只想找到 showChild 为 'Y' 的行的 cat1、cat2、cat3 和总计的总计(即)聚合,并将其添加到结果集中,是否可能?

|Tot| Res | Res | N | 10 | 10 | 10 | 30 |

预期最终输出:

|FILE| ID |PARENTID|SHOWCHILD|CAT1|CAT2|CAT3|TOTAL|
|F1  | A1 |  P1    |     N   | 3  | 2  | 6  | 11  |
|F2  | A2 |  P2    |     N   | 4  | 7  | 3  | 14  |
|F3  | A3 |  P1    |     N   | 3  | 1  | 1  | 5   |
|F4  | LG1|        |     Y   | 6  | 3  | 7  | 16  |
|F5  | LG2|        |     Y   | 4  | 7  | 3  | 14  |
|Tot | Res|  Res   |     N   | 10 | 10 | 10 | 30  |

在这里我添加了 Tot 行(最后一行),仅考虑了将 showchild 作为 'Y' 的行并将其添加到结果集中。

我正在尝试不使用 UNION

的解决方案

非常感谢任何有助于实现上述结果的帮助。

谢谢。

一种方法是使用联合:

WITH cte AS (
    SELECT "FILE", ID, PARENTID, SHOWCHILD, CAT1, CAT2, CAT3, TOTAL, 1 AS position
    FROM yourTable
    UNION ALL
    SELECT 'Tot', 'Res', 'Res', 'N', SUM(CAT1), SUM(CAT2), SUM(CAT3), SUM(TOTAL), 2
    FROM yourTable
    WHERE SHOWCHILD = 'Y'
)

SELECT "FILE", ID, PARENTID, SHOWCHILD, CAT1, CAT2, CAT3, TOTAL
FROM cte
ORDER BY
    position,
    "FILE";

Demo

您可以尝试使用 UNION

select FILE,ID ,PARENTID,SHOWCHILD,CAT1,CAT2,CAT3,TOTAL from table1
union 
select 'Tot','Res','Res','N',sum(cat1), sum(cat2),sum(cat3), sum(total)
from table1 where SHOWCHILD='Y'

我看到你已经接受了一个答案,但是你确实要求了一个不涉及UNION的解决方案。一种这样的解决方案是使用 GROUPING SETS.

GROUPING SETS 允许您在查询中指定不同的分组级别并在每个级别生成聚合。根据您的要求,您可以使用它为每条记录加上单个 "total" 行生成一个输出行。函数 GROUPING 可用于表达式中,以识别每个输出行是在一组还是另一组中。

示例,带有测试数据:

with input_data ("FILE", "ID", PARENTID, SHOWCHILD, CAT1, CAT2, CAT3, TOTAL ) AS (
SELECT 'F1','A1','P1','N',3,2,6,11 FROM DUAL UNION ALL
SELECT 'F2','A2','P2','N',4,7,3,14 FROM DUAL UNION ALL
SELECT 'F3','A3','P1','N',3,1,1,5 FROM DUAL UNION ALL
SELECT 'F4','LG1','','Y',6,3,7,16 FROM DUAL UNION ALL
SELECT 'F5','LG2','','Y',4,7,3,14 FROM DUAL )
SELECT decode(grouping("FILE"),1,'Tot',"FILE") "FILE", 
       decode(grouping("ID"),1,'Res',"ID") "ID",
       decode(grouping(parentid),1, 'Res',parentid) parentid, 
       decode(grouping(showchild),1, 'N',showchild) showchild, 
       decode(grouping("FILE"),1,sum(decode(showchild,'Y',cat1,0)),sum(cat1)) cat1, 
       decode(grouping("FILE"),1,sum(decode(showchild,'Y',cat2,0)),sum(cat2)) cat2, 
       decode(grouping("FILE"),1,sum(decode(showchild,'Y',cat3,0)),sum(cat3)) cat3, 
       decode(grouping("FILE"),1,sum(decode(showchild,'Y',total,0)),sum(total)) total 
from input_data
group by grouping sets (("FILE", "ID", parentid, showchild), ())
+------+-----+-----+----------+-----------+------+------+------+-------+
| FILE | F2  | ID  | PARENTID | SHOWCHILD | CAT1 | CAT2 | CAT3 | TOTAL |
+------+-----+-----+----------+-----------+------+------+------+-------+
| F1   | F1  | A1  | P1       | N         |    3 |    2 |    6 |    11 |
| F2   | F2  | A2  | P2       | N         |    4 |    7 |    3 |    14 |
| F3   | F3  | A3  | P1       | N         |    3 |    1 |    1 |     5 |
| F4   | F4  | LG1 |  -       | Y         |    6 |    3 |    7 |    16 |
| F5   | F5  | LG2 |  -       | Y         |    4 |    7 |    3 |    14 |
| Tot  | Tot | Res | Res      | N         |   10 |   10 |   10 |    30 |
+------+-----+-----+----------+-----------+------+------+------+-------+