WITH t AS
(SELECT 'A' CODE, 10 amt FROM dual UNION ALL
SELECT 'E', 20 FROM dual UNION ALL
SELECT 'R', 30 FROM dual UNION ALL
SELECT 'T', 40 FROM dual UNION ALL
SELECT 'C', 50 FROM dual UNION ALL
SELECT 'S', 60 FROM dual UNION ALL
SELECT 'V', 70 FROM dual )
SELECT *
FROM t
위와 같은 샘플 데이터를 만들어 보았습니다.
하고자 하는것은 표시되는 code와 amt 컬럼을 두줄 단위를 한줄로 code order 기준으로
좌우로 아래와 같이 표시하고 싶습니다.
--> 조회 결과
A 10 C 50
E 20 R 30
S 60 T 40
V 70
--max decode WITH t AS (SELECT 'A' CODE, 10 amt FROM dual UNION ALL SELECT 'E', 20 FROM dual UNION ALL SELECT 'R', 30 FROM dual UNION ALL SELECT 'T', 40 FROM dual UNION ALL SELECT 'C', 50 FROM dual UNION ALL SELECT 'S', 60 FROM dual UNION ALL SELECT 'V', 70 FROM dual ) SELECT MAX(DECODE(GB2,1,CODE)) GB1_CODE, MAX(DECODE(GB2,1,AMT)) GB1_AMT , MAX(DECODE(GB2,2,CODE)) GB2_CODE, MAX(DECODE(GB2,2,AMT)) GB2_AMT FROM ( SELECT CODE, AMT, GB1, ROW_NUMBER() OVER(PARTITION BY GB1 ORDER BY CODE) GB2 FROM ( SELECT CODE, AMT, CEIL(ROW_NUMBER() OVER(ORDER BY CODE)/2) GB1 FROM t ) ) GROUP BY GB1 --11g pivot SELECT GB1_CODE, GB1_AMT, GB2_CODE, GB2_AMT FROM ( SELECT CODE, AMT, GB1, ROW_NUMBER() OVER(PARTITION BY GB1 ORDER BY CODE) GB2 FROM ( SELECT CODE, AMT, CEIL(ROW_NUMBER() OVER(ORDER BY CODE)/2) GB1 FROM t ) ) PIVOT (MAX(CODE) CODE, MAX(AMT) AMT FOR GB2 IN (1 AS "GB1",2 AS "GB2"))