drop table 월별지점매출 purge ;

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.판매월 ;

 

누적매출인 b.매출의 합계는 a.판매월 보다 작거나 같아야 한다.

예를들어 3월보다 작아야 3월까지의 누적매출 합계가 만들어진다.

SMALL

'Oracle' 카테고리의 다른 글

Real-Time SQL Monitoring  (0) 2020.10.19
Database Sample Schemas  (0) 2020.09.26
Oracle_Home 경로 확인  (0) 2020.08.08
SQLP - SQL자격검정실전문제 실기문제2 풀이  (0) 2020.06.30
Oracle 히든파라미터 조회방법  (0) 2020.06.26

+ Recent posts