
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
안녕하세요 5주차 숙제 질문합니다.
제가 처음에 쿼리를 작성할때 max수식에서 연령을 between으로 구간을 구분하였는데,
정답과는 다른 결과가 나왔습니다. 어디서 차이가 발생한 건가요?
작성한 코드 및 에러 메세지
<초기작성>
SELECT cuisine_type,
max(if (age between 10 and 19,주문건수,0)) "10대",
max(if (age between 20 and 29,주문건수,0)) "20대",
max(if (age between 30 and 39,주문건수,0)) "30대",
max(if (age between 40 and 49,주문건수,0)) "40대",
max(if (age between 50 and 59,주문건수,0)) "50대"
from
(SELECT cuisine_type , age, count(1) "주문건수"
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
<정답>
SELECT cuisine_type,
MAX(if(연령=10, 주문건수,0)) "10대",
MAX(if(연령=20, 주문건수,0)) "20대",
MAX(if(연령=30, 주문건수,0)) "30대",
MAX(if(연령=40, 주문건수,0)) "40대",
MAX(if(연령=50, 주문건수,0)) "50대"
from
(SELECT 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 59 end "연령",
count(1) "주문건수"
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
