create table 월별지점매출 ( 지점 number , 판매월 number , 매출 number ) ;
insert into 월별지점매출 select 10, 1, 521 from dual union all select 10, 2, 684 from dual union all select 10, 3, 590 from dual union all select 20, 1, 537 from dual union all select 20, 2, 650 from dual union all select 20, 3, 500 from dual union all select 20, 4, 919 from dual union all select 20, 5, 658 from dual union all select 30, 1, 631 from dual union all select 30, 2, 736 from dual union all select 30, 3, 513 from dual union all select 30, 4, 970 from dual union all select 30, 5, 939 from dual union all select 30, 6, 666 from dual ;
select * from 월별지점매출 ;
각 지점별로 판매월과 함께 증가하는 누적매출을 구하는 SQL 을 아래 두가지 방식으로 작성하시오.
1) 윈도우함수를 이용한 방식으로 작성
2) 윈도우함수나 스칼라서브쿼리를 지원하지않는 DBMS에서 활용할 수 있는 방식으로 작성
아래와 같은결과를 만드시오.
1)
select a.지점 , a.판매월 , a.매출 , sum(a.매출) over (partition by 지점 order by 판매월 ) 누적매출 from 월별지점매출 a ;
윈도우함수는 partition by절을 정확하게 작성하는것이 중요하다.
sum(a.매출) over (partition by 지점 order by 판매월 )
= sum(a.매출) over (partition by 지점 order by 판매월 range between unbounded preceding and current row)
2)
select a.지점 , a.판매월 , a.매출 , sum(b.매출) 누적매출 from 월별지점매출 a inner join 월별지점매출 b on a.지점 = b.지점 and a.판매월 >= b.판매월 group by a.지점, a.판매월, a.매출 order by a.지점, a.판매월 ;