[답변]부탁드립니다...T.T 0 1 1,439

by 이주호 [2007.05.02 16:21:53]


뒷북이네요. 아래 참고하세요.

 

with t as (
select 'AA' 품번, '200611' 적용년월, 200 단가 from dual union all
select 'AA',      '200703',          210 단가 from dual union all
select 'AB',      '200701',          180 단가 from dual union all
select 'AC',      '200605',          130 단가 from dual union all
select 'AC',      '200702',          140 단가 from dual union all
select 'AC',      '200709',          150 단가 from dual
)
select 품번
       ,sum(case when 적용년월 <= '200701' and '200701' < 다음적용년월 then 단가 end) mon01
       ,sum(case when 적용년월 <= '200702' and '200702' < 다음적용년월 then 단가 end) mon02
       ,sum(case when 적용년월 <= '200703' and '200703' < 다음적용년월 then 단가 end) mon03
       ,sum(case when 적용년월 <= '200704' and '200704' < 다음적용년월 then 단가 end) mon04
       ,sum(case when 적용년월 <= '200705' and '200705' < 다음적용년월 then 단가 end) mon05
       ,sum(case when 적용년월 <= '200706' and '200706' < 다음적용년월 then 단가 end) mon06
       ,sum(case when 적용년월 <= '200707' and '200707' < 다음적용년월 then 단가 end) mon07
       ,sum(case when 적용년월 <= '200708' and '200708' < 다음적용년월 then 단가 end) mon08
       ,sum(case when 적용년월 <= '200709' and '200709' < 다음적용년월 then 단가 end) mon09
       ,sum(case when 적용년월 <= '200710' and '200710' < 다음적용년월 then 단가 end) mon10
       ,sum(case when 적용년월 <= '200711' and '200711' < 다음적용년월 then 단가 end) mon11
       ,sum(case when 적용년월 <= '200712' and '200712' < 다음적용년월 then 단가 end) mon12
from (
        select 품번
               ,적용년월
               ,case when lead(적용년월) over (partition by 품번 order by 적용년월) is null then '999999'
                     else lead(적용년월) over (partition by 품번 order by 적용년월)
                end 다음적용년월
               ,단가
        from t      
      ) t
group by 품번

by 마농 [2007.05.02 00:00:00]
인라인뷰의 case 절은 다음처럼 간단하게 바꿀 수 있습니다.
lead(적용년월,1,'999999') over (partition by 품번 order by 적용년월) 다음적용년월
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입