커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
5주차 마지막 숙제 관련
엑셀보다 쉽고 빠른 SQL v0
5주차
북마크
김*원
댓글
6
추천
0
조회수
36
조회수
36
답변 완료

숙제를 제출했는데, 뭐가 잘못된지 모르겠습니다. ㅠㅠ

결과에서 보여준 스샷 결과 이미지랑 제가 SQL 쿼리 짠거랑 결과 값이 달라서요.


연령별/음식타입별 주문건수를 연령대별로 피벗테이블 형태로 보여주기 (20대~50대로 구분지어서 구함)


#제가 작성 한 것

select cuisine_type,

max(if(sum_age='20대',cnt_order,0)) "20대",

max(if(sum_age='30대',cnt_order,0)) "30대",

max(if(sum_age='40대',cnt_order,0)) "40대",

max(if(sum_age='50대',cnt_order,0)) "50대"

from

(

select cuisine_type,

cnt_order,

case when age between 20 and 29 then '20대'

when age between 30 and 39 then '30대'

when age between 40 and 49 then '40대'

when age between 50 and 59 then '50대'

else 'etc' end 'sum_age'

from

(

select fo.cuisine_type,

c.age,

count(1) as cnt_order

from food_orders fo left join customers c on fo.customer_id = c.customer_id

where age between 20 and 60

group by cuisine_type, age

)a

)b

group by 1

order by 1


스파르타 즉문즉답


----------------------------------------------------------------------

#정답


select cuisine_type,

max(if(age=10, order_count, 0)) "10대",

max(if(age=20, order_count, 0)) "20대",

max(if(age=30, order_count, 0)) "30대",

max(if(age=40, order_count, 0)) "40대",

max(if(age=50, order_count, 0)) "50대"

from

(

select a.cuisine_type,

case when age between 10 and 19 then 10

when age between 20 and 29 then 20

when age between 30 and 39 then 30

when age between 40 and 49 then 40

when age between 50 and 59 then 50 end age,

count(1) order_count

from food_orders a inner join customers b on a.customer_id=b.customer_id

where age between 10 and 59

group by 1, 2

) t

group by 1

order by 1

스파르타 즉문즉답


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴