1. 비동치 조인을 이용한 GROUP BY 누산
WITH TBL
AS ( SELECT 'A' AS COL1, 10 AS COL2 FROM DUAL
UNION ALL
SELECT 'B' AS COL1, 20 AS COL2 FROM DUAL
UNION ALL
SELECT 'C' AS COL1, 10 AS COL2 FROM DUAL
UNION ALL
SELECT 'D' AS COL1, 30 AS COL2 FROM DUAL
)
SELECT GROUPING_ID(T1.COL1) AS IS_TOTAL
, T1.COL1
, T1.COL2
, SUM(T2.COL2) AS ACC_SUM
FROM TBL T1
JOIN TBL T2 ON T1.COL1 >= T2.COL1
GROUP BY GROUPING SETS ((T1.COL1, T1.COL2), ());
IS_TOTAL COL1 COL2 ACC_SUM
-------- ---- ---- -------
0 A 10 10
0 B 20 30
0 C 10 40
0 D 30 70
1 150
2. 단순 SUM을 이용한 누산
WITH TBL
AS ( SELECT 'A' AS COL1, 10 AS COL2 FROM DUAL
UNION ALL
SELECT 'B' AS COL1, 20 AS COL2 FROM DUAL
UNION ALL
SELECT 'C' AS COL1, 10 AS COL2 FROM DUAL
UNION ALL
SELECT 'D' AS COL1, 30 AS COL2 FROM DUAL
)
SELECT T1.COL1
, T1.COL2
, SUM(T1.COL2) OVER (ORDER BY T1.COL1) AS ACC_SUM
FROM TBL T1;
COL1 COL2 ACC_SUM
---- ---- -------
A 10 10
B 20 30
C 10 40
D 30 70