본문 바로가기

02. SQLP 스터디/03. 기출

SQLP 48-1

  https://velog.io/@jkjan/%EC%A0%9C-48%ED%9A%8C-SQLP-%EC%8B%A4%EA%B8%B0-%EB%B3%B5%EA%B8%B0

* 문제 쿼리

select 
    '일별' 기간구분,
    판매일시,
    고객등급,
    sum(판매금액) 판매금액
from (
    select 
        substr(sale_dt, 1, 8) 판매일시,
        fnc_get_cust_grade(cust_id) 고객등급,
        sale_amt 판매금액
    from 
        sale
    where
        sale_dt between '20220101' and '20220731'
    )
group by 판매일시, 고객등급
union all
select 
    '월별' 기간구분,
    판매일시,
    고객등급,
    sum(판매금액) 판매금액
from (
    select 
        substr(sale_dt, 1, 6) 판매일시,
        fnc_get_cust_grade(cust_id) 고객등급,
        sale_amt 판매금액
    from 
        sale
    where
        sale_dt between '20220101' and '20220731'
    )
group by 판매일시, 고객등급
order by 1, 2, 3
;

 

* 제출 쿼리

select 
    case when 일 is not null then '일별' else '월별' end 기간구분,
    case when 일 is not null then 일 else 월 end 판매일시,
    고객등급, 
    판매금액
from (
    select 일, 월, 고객등급, sum(판매금액) 판매금액
    from (
        select 
            substr(sale_dt, 1, 8) 일,
            substr(sale_dt, 1, 6) 월,
            sale_amt 판매금액,
            cust_grade 고객등급
        from
            sale s, customer c
        where
            s.cust_id = c.cust_id 
            and sale_dt between '20220101' and '20220731'
    )
    group by grouping sets((일, 고객등급), (월, 고객등급))
)
order by 1, 2, 3

select decode(c.no, 1, '일별','월별) 주문유형 ,decode(c.no, 1, b.주문일자, b.주문월) 주문구분,b.고객등급,sum(b.주문금액) 주문금액

from (

select substr(a.주문일시,1,8) 주문일자 , substr(a.주문일시,1,6) 주문월 , a.고객번호, sum(a.주문금액) 주문금액

from 주문 a

where 주문일시 between '~~' and '~~' -- 조건 생각 안 남

group by substr(a.주문일시,1,8) 주문일자 , substr(a.주문일시,1,6) 주문월 , a.고객번호 ) a1 ,

고객 b ,

(select level as no from dual connect by level <=2 ) c

where a1.고객번호 = b.고객번호

group by decode(c.no, 1, '일별','월별) 주문유형 ,decode(c.no, 1, b.주문일자, b.주문월) 주문구분,b.고객등급 ;
 
 

1번

다음 쿼리를 재작성하라.

문제 쿼리

 
 
 
 

샘플 데이터 생성 쿼리

 
 

 
 
 

'02. SQLP 스터디 > 03. 기출' 카테고리의 다른 글

SQLP 47  (0) 2025.04.07
SQLP 48-2  (0) 2025.04.07
SQLP 49-2  (0) 2025.04.06
SQLP 49-1  (0) 2025.04.06
SQLP 50회 -2  (0) 2025.04.06