
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
SQL 강의 5주차 숙제 질문이 있어요.
제가 한 것처럼 10살~59살 까지 가져온 후에 앞글자만 따와 1~5인지 구분해서 연령대를 구별하는것과
정답코드처럼 서브쿼리문 select 부분에서 case 문을 사용해 연령대 구분해주는 것이 실행 결과가 다른데 왜 그런건지 궁금해요~

작성한 코드 및 에러 메세지
제가 작성한 코드입니다.
SELECT cuisine_type,
max(if(substr(age, 1,1)='1', cnt_order,0)) "10대",
max(if(substr(age, 1,1)='2', cnt_order,0)) "20대",
max(if(substr(age, 1,1)='3', cnt_order,0)) "30대",
max(if(substr(age, 1,1)='4', cnt_order,0)) "40대",
max(if(substr(age, 1,1)='5', cnt_order,0)) "50대"
from
(
select f.cuisine_type,
c.age,
count(1) cnt_order
from food_orders f left join customers c on f.customer_id=c.customer_id
where c.age between 10 and 59
group by 1,2
)a
group by 1
아래는 5주차 정답코드입니다
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
