SELECT * FROM ( SELECT '001' AS ANO, '002' AS VNO, 220 AS CNT FROM DUAL UNION ALL SELECT '002' AS ANO, '001' AS VNO, 140 AS CNT FROM DUAL UNION ALL SELECT '003' AS ANO, '004' AS VNO, 2220 AS CNT FROM DUAL UNION ALL SELECT '004' AS ANO, '003' AS VNO, 2140 AS CNT FROM DUAL ) ANO VNO CNT --- --- ----------- 001 002 220 002 001 140 003 004 2220 004 003 2140 위와 같은 데이터에서 아래와 같은 데이터를 뽑을 수 있을까요? ANOVNO VNOANO CNT ------ ------ ----- 001002 002001 360 003004 004003 4360 001002 는 ANO || VNO 로 합쳐 놓은겁니다.
WITH T AS ( SELECT '001' AS ANO, '002' AS VNO, 220 AS CNT FROM DUAL UNION ALL SELECT '002' AS ANO, '001' AS VNO, 140 AS CNT FROM DUAL UNION ALL SELECT '003' AS ANO, '004' AS VNO, 2220 AS CNT FROM DUAL UNION ALL SELECT '004' AS ANO, '003' AS VNO, 2140 AS CNT FROM DUAL ) SELECT A.V1 , A.V2 , ( A.CNT + B.CNT ) AS CNT FROM (SELECT ANO || VNO AS V1 , VNO || ANO AS V2 , CNT FROM T ) A , (SELECT ANO || VNO AS V1 , VNO || ANO AS V2 , CNT FROM T ) B WHERE A.V1 = B.V2 AND B.V2 = A.V1 AND A.V1 >= B.V1
WITH T AS ( SELECT '001' AS ANO, '002' AS VNO, 220 AS CNT FROM DUAL UNION ALL SELECT '002' AS ANO, '001' AS VNO, 140 AS CNT FROM DUAL UNION ALL SELECT '003' AS ANO, '004' AS VNO, 2220 AS CNT FROM DUAL UNION ALL SELECT '004' AS ANO, '003' AS VNO, 2140 AS CNT FROM DUAL ) SELECT ANOVNO, VNOANO, SUM(CNT) CNT FROM (SELECT MIN(ANO||VNO) OVER(PARTITION BY BASIS) ANOVNO, MAX(ANO||VNO) OVER(PARTITION BY BASIS) VNOANO, CNT FROM (SELECT LEAST(ANO, VNO) BASIS, ANO, VNO, CNT FROM T ) ) GROUP BY ANOVNO, VNOANO
WITH t AS ( SELECT '001' ano, '002' vno, 220 cnt FROM dual UNION ALL SELECT '002', '001', 140 FROM dual UNION ALL SELECT '003', '004', 2220 FROM dual UNION ALL SELECT '004', '003', 2140 FROM dual ) SELECT ano||vno anovno , vno||ano vnoano , SUM(cnt) cnt FROM (SELECT LEAST (ano, vno) ano , GREATEST(ano, vno) vno , cnt FROM t ) GROUP BY ano, vno ORDER BY ano, vno ;