DATE | 2000-01-01 | ||
DATE | 2019-02-02 | ||
DATE | 2019-02-05 | ||
DATE | 2019-02-05 | ||
DATE | 2019-02-12 | ||
DATE | 2019-02-20 | ||
DATE | 2019-02-05 | ||
DATE | 3030-01-01 | ||
DATE | 2019-02-28 | ||
DATE | 2019-02-28 | ||
DATE2 | 2018-09-01 | ||
DATE2 | 2019-02-01 | ||
DATE2 | 2019-02-07 | ||
DATE2 | 2016-02-15 | ||
DATE2 | 2016-03-01 | ||
DATE2 | 2016-03-15 | ||
DATE2 | 2016-03-28 |
위에 데이터를 아래 처럼 만들고 싶은데 어떻게 해야 할까요? ㅠㅠ
DATE | 2000-01-01 | 2019-02-05 | 2019-02-05 |
DATE | 2019-02-02 | 2019-02-05 | 3030-01-01 |
DATE | 2019-02-12 | 2019-02-28 | |
DATE | 2019-02-20 | 2019-02-28 | |
DATE2 | 2018-09-01 | 2019-02-07 | 2016-02-15 |
DATE2 | 2019-02-01 | 2016-03-01 | |
DATE2 | 2016-03-15 | ||
DATE2 | 2016-03-28 |
WITH T AS (
SELECT 'DATE' AS DT , '2000-01-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , '2000-02-02' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, '2019-02-05' COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, '2019-02-05' COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, '2019-02-12' COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, '2019-02-20' COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-05' COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '3030-01-01' COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-28' COL3 FROM DUAL UNION ALL
SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-28' COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , '2018-09-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , '2019-02-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , NULL COL1, '2019-02-07' COL2, NULL COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-02-15' COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-01' COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-15' COL3 FROM DUAL UNION ALL
SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-28' COL3 FROM DUAL
)
SELECT * FROM T
오라클은 안 써봤지만.. 이런 식으로 해볼 수 있을 듯 하네요.
WITH T AS ( SELECT 'DATE' AS DT , '2000-01-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , '2000-02-02' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, '2019-02-05' COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, '2019-02-05' COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, '2019-02-12' COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, '2019-02-20' COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-05' COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '3030-01-01' COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-28' COL3 FROM DUAL UNION ALL SELECT 'DATE' AS DT , NULL COL1, NULL COL2, '2019-02-28' COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , '2018-09-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , '2019-02-01' COL1, NULL COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , NULL COL1, '2019-02-07' COL2, NULL COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-02-15' COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-01' COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-15' COL3 FROM DUAL UNION ALL SELECT 'DATE2' AS DT , NULL COL1, NULL COL2, '2016-03-28' COL3 FROM DUAL ), C1 (SEQ, DT, COL1) AS ( SELECT ROW_NUMBER() OVER(PARTITION BY DT ORDER BY COL1), DT, COL1 FROM T WHERE COL1 IS NOT NULL ), C2 (SEQ, DT, COL2) AS ( SELECT ROW_NUMBER() OVER(PARTITION BY DT ORDER BY COL2), DT, COL2 FROM T WHERE COL2 IS NOT NULL ), C3 (SEQ,DT, COL3) AS SELECT ROW_NUMBER() OVER(PARTITION BY DT ORDER BY COL3), DT, COL3 FROM T WHERE COL3 IS NOT NULL ) SELECT COALESCE(C1.DT, C2.DT, C3.DT) AS DT, C1.COL1, C2.COL2, C3.COL3 FROM C1 FULL OUTER JOIN C2 ON C2.DT = C1.DT AND C2.SEQ = C1.SEQ FULL OUTER JOIN C3 ON C3.DT = C2.DT AND C3.SEQ = C2.SEQ ORDER BY COALESCE(C1.DT, C2.DT, C3.DT) , COALESCE(C1.SEQ, C2.SEQ, C3.SEQ);
select * from (select dt, dts , gb , row_number() over(partition by dt, gb order by rn) pgb from (select dt , col1||col2||col3 as dts , case when col1 is not null then 1 when col2 is not null then 2 when col3 is not null then 3 end gb , rownum rn from t ) ) pivot(min(dts) as dtss for(gb) in (1, 2, 3)) order by dt, pgb ;