• 카테고리

    질문 & 답변
  • 세부 분야

    데이터베이스

  • 해결 여부

    미해결

Group by rollup 실습 마지막 예제 쿼리 질문

22.07.11 15:11 작성 조회수 120

0

총매출/연매출/월매출을 구분하기 위해 case when 구문을 사용하셨는데

연/월 매출을 구할 때 수업 내 쿼리처럼 else를 다중으로 적는 것이 아닌

and로 else를 축약해도 쿼리상 문제가 없는지 궁금합니다.




with cte1 as (select to_char(order_date,'yyyy') as year

, to_char(order_date, 'mm') as month

, to_char(order_date, 'dd') as day

,sum(b.amount)  as sum_amount

from orders as a 

join order_items as b on a.order_id = b.order_id 

group by rollup (1,2,3)

order by 1,2,3

)

select case when year is null then '총 매출' else year end as year

, case when year is not null and month is null then '년 매출' else month end as month

, case when year is not null and month is not null and day is null then '월 매출' else day end as day

,sum_amount

from cte1

답변 1

답변을 작성해보세요.

2

안녕하십니까, 

네, 그렇게 하셔도 됩니다. 

제 코드보다 더 깔끔한거 같습니다 ^^

감사합니다.