커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
숙제에서 MAX() 함수를 사용하는 이유에 대해서 질문 드립니다.
엑셀보다 쉽고 빠른 SQL
5주차
북마크
온*윤
댓글
3
추천
0
조회수
29
조회수
29
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.



저는 MAX()를 안쓰고 SUM()을 사용하여 쿼리문을 작성했는데, 결과적으로 정답과 같은 결과가 나왔습니다.

select

cuisine_type,

sum(if(age between 10 and 19, cnt_order, 0)) '10대',

sum(if(age between 20 and 29, cnt_order, 0)) '20대',

sum(if(age between 30 and 39, cnt_order, 0)) '30대',

sum(if(age between 40 and 49, cnt_order, 0)) '40대',

sum(if(age between 50 and 59, cnt_order, 0)) '50대'

from

(

select

cuisine_type,

c.age,

count(1) cnt_order

from

food_orders f inner join customers c on f.customer_id = c.customer_id

where age between 10 and 59

GROUP by

1, 2

) a

group by

1


정답에서 max()를 사용할 수 있었던 이유는

서브쿼리에서 group by 1,2로 묶어주었기 때문에 각 음식 타입별, 연령대별로 값이 1개밖에 없기 때문으로 이해했는데 맞나요?

아니라면 왜 max를 사용했는지 잘 이해가 가지 않아서 설명 도와주시면 감사하겠습니다..!


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








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