안녕하세요, 쿼리 작성중 막혀 질문드립니다. ㅠㅠ
year | month | num_of_pers | prvyy_versus |
2004 | 1 | 164785 | |
2004 | 2 | 142718 | |
2004 | 3 | 112516 | |
2004 | 4 | 120427 | |
2004 | 5 | 115659 | |
2004 | 6 | 116269 | |
2004 | 7 | 160770 | |
2004 | 8 | 170182 | |
2004 | 9 | 113083 | |
2004 | 10 | 122877 | |
2004 | 11 | 128369 | |
2004 | 12 | 120817 | |
2005 | 1 | 174775 | 0.06062445 |
2005 | 2 | 148946 | 0.043638504 |
2005 | 3 | 130963 | 0.163950016 |
2005 | 4 | 122084 | 0.013759373 |
2005 | 5 | 114151 | -0.013038328 |
2005 | 6 | 133177 | 0.145421393 |
2005 | 7 | 170420 | 0.060023636 |
2005 | 8 | 193279 | 0.135719406 |
2005 | 9 | 130269 | 0.151976867 |
2005 | 10 | 146650 | 0.193469893 |
2005 | 11 | 140442 | 0.094049186 |
2005 | 12 | 142015 | 0.175455441 |
현재 db에 이런 데이터가 들어있는데
01월 | 02월 | 03월 | 04월 | 05월 | ||||||
명수 | 전년대비 | 명수 | 전년대비 | 명수 | 전년대비 | 명수 | 전년대비 | 명수 | 전년대비 | |
2004 | 123123 | 34 |
이런 형식으로 테이블을 뿌리고 싶은데 쿼리를 어떻게 짜야 할까요.. 검색해본 결과 피벗을 사용해야 하는거 같은데
2004 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
2005 |
|
이렇게 결과가 나와야 할까요? 질문드립니다 감사합니다!
WITH t AS ( select 2004 year, 1 month, 164785 v_amt, 0 v_rate from dual union all select 2004, 2, 142718, 0 from dual union all select 2004, 3, 112516, 0 from dual union all select 2004, 4, 120427, 0 from dual union all select 2004, 5, 115659, 0 from dual union all select 2004, 6, 116269, 0 from dual union all select 2004, 7, 160770, 0 from dual union all select 2004, 8, 170182, 0 from dual union all select 2004, 9, 113083, 0 from dual union all select 2004, 10, 122877, 0 from dual union all select 2004, 11, 128369, 0 from dual union all select 2004, 12, 120817, 0 from dual union all select 2005, 1, 174775, 0.06062445 from dual union all select 2005, 2, 148946, 0.043638504 from dual union all select 2005, 3, 130963, 0.163950016 from dual union all select 2005, 4, 122084, 0.013759373 from dual union all select 2005, 5, 114151, -0.013038328 from dual union all select 2005, 6, 133177, 0.145421393 from dual union all select 2005, 7, 170420, 0.060023636 from dual union all select 2005, 8, 193279, 0.135719406 from dual union all select 2005, 9, 130269, 0.151976867 from dual union all select 2005, 10, 146650, 0.193469893 from dual union all select 2005, 11, 140442, 0.094049186 from dual union all select 2005, 12, 142015, 0.175455441 from dual) SELECT year , max(case when month = 1 then v_amt else 0 end) , max(case when month = 1 then v_rate else 0 end) , max(case when month = 2 then v_amt else 0 end) , max(case when month = 2 then v_rate else 0 end) , max(case when month = 3 then v_amt else 0 end) , max(case when month = 3 then v_rate else 0 end) , max(case when month = 4 then v_amt else 0 end) , max(case when month = 4 then v_rate else 0 end) , max(case when month = 5 then v_amt else 0 end) , max(case when month = 5 then v_rate else 0 end) , max(case when month = 6 then v_amt else 0 end) , max(case when month = 6 then v_rate else 0 end) , max(case when month = 7 then v_amt else 0 end) , max(case when month = 7 then v_rate else 0 end) , max(case when month = 8 then v_amt else 0 end) , max(case when month = 8 then v_rate else 0 end) , max(case when month = 9 then v_amt else 0 end) , max(case when month = 9 then v_rate else 0 end) , max(case when month = 10 then v_amt else 0 end) , max(case when month = 10 then v_rate else 0 end) , max(case when month = 11 then v_amt else 0 end) , max(case when month = 11 then v_rate else 0 end) , max(case when month = 12 then v_amt else 0 end) , max(case when month = 12 then v_rate else 0 end) FROM t group by year order by year