데이터가
1 2 3 4 5 6 7 8 9 10 11 12 13 | code name month val 00 hello 01 4 01 hi 01 4 02 meet 01 3 03 04 ........ 00 hello 02 2 01 hi 02 3 02 meet 02 3 03 04 ......... |
code 데이터는 제한이 없구요 month는 12월까지 데이터가 있습니다.
이렇게 들어 있습니다.
이것을
1 2 3 4 5 | code name month_01_ val month_02_val ..... month_12_val 00 hello 4 2 01 hi 4 3 02 meet 3 3 .......... |
이렇게 하고싶습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | WITH TT(code, name , month , val) AS ( SELECT '00' , 'hello' , '01' , 4 FROM DUAL UNION ALL SELECT '01' , 'hi' , '01' , 4 FROM DUAL UNION ALL SELECT '02' , 'meet' , '01' , 3 FROM DUAL UNION ALL SELECT '00' , 'hello' , '02' , 2 FROM DUAL UNION ALL SELECT '01' , 'hi' , '02' , 3 FROM DUAL UNION ALL SELECT '02' , 'meet' , '02' , 3 FROM DUAL ) SELECT * FROM TT PIVOT ( SUM (val) FOR month IN ( '01' month_01_val, '02' month_02_val, '03' month_03_val , '04' month_04_val, '05' month_05_val, '06' month_06_val , '07' month_07_val, '08' month_08_val, '09' month_09_val , '10' month_10_val, '11' month_11_val, '12' month_12_val) ) ORDER BY CODE ; |