인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

ajh7457님의 프로필 이미지
ajh7457

작성한 질문수

데이터 분석 SQL Fundamentals

Group By Rollup 실습

rollup

작성

·

318

0

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.

 

with temp_01 as

(select to_char(b.order_date, 'yyyy') as year

, to_char(b.order_date, 'mm') as month

, to_char(b.order_date, 'dd') as day

, sum(a.amount) as sum_amount

from nw.order_items a

join nw.orders b on a.order_id = b.order_id

group by rollup(to_char(b.order_date, 'yyyy'), to_char(b.order_date, 'mm'), to_char(b.order_date, 'dd'))

order by 1, 2, 3)

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

case when month = null then month ='년 총매출' else month end as month,

case when day = null then day = '월 총매출' else day end as day,

sum_amount

from temp_01

order by year, month, day

;

 

이 구문은 어디가 틀렸나요?

답변 1

0

권 철민님의 프로필 이미지
권 철민
지식공유자

안녕하십니까,

아래에서 두가지가 잘못 되었습니다.

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

case when month = null then month ='년 총매출' else month end as month,

case when day = null then day = '월 총매출' else day end as day,

첫번째는 null 값은 = 로 비교될 수 없습니다. null 값은 is null로 찾아야 합니다.

두번째는 case when 절의 then에 year = '총매출' 같이 다시 조건 값을 해주려면 then case when을 사용해야 합니다. 그게 아니라면 then '총매출'과 같이 최종 결정값을 설정해 줘야 합니다.

따라서 위 SQL은 아래와 같이 변경되면 될 것 같습니다.

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

case when month is null then '년 총매출' else month end as month,

case when day is null then '월 총매출' else day end as day,

감사합니다.

ajh7457님의 프로필 이미지
ajh7457
질문자

초면에 사랑합니다❤️

권 철민님의 프로필 이미지
권 철민
지식공유자

ㅋㅋ 감사합니다

ajh7457님의 프로필 이미지
ajh7457

작성한 질문수

질문하기