Просто идея:
WITH test_data AS
(
SELECT 'A' no1, 'B' no2, 10 amt FROM dual
UNION ALL
SELECT 'C', 'D', 20 FROM dual
UNION ALL
SELECT 'B', 'A', 30 FROM dual
UNION ALL
SELECT 'D', 'C', 40 FROM dual
)
SELECT NVL(no1, break_group) no1, no2, total --, grp1, grp2, break_group
FROM
(
SELECT no1, no2, total
, grouping(no1) grp1
, grouping(no2) grp2
, LAG(no1, 2) OVER (PARTITION BY total ORDER BY no1)||LAG(no1) OVER (PARTITION BY total ORDER BY no1) break_group
FROM
( -- Vamsi Prabhala query --
select no1, no2
, sum(amt) over(partition by least(no1,no2),greatest(no1,no2)) as total
from test_data
)
GROUP BY ROLLUP (total, no1, no2)
HAVING grouping(no1) + grouping(no2) = 0
OR grouping(no1) + grouping(no2) = 2
AND total IS NOT NULL
)
/
Вывод - вы можете заменить null (пусто) на любой vlue:
|NO1 | NO2 | Total
----------------------
A B 40
B A 40
AB 40
C D 60
D C 60
CD 60