커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
case when과 group by 질문
엑셀보다 쉽고 빠른 SQL
챕터 5
북마크
홍*지
댓글
6
추천
0
조회수
27
조회수
27
답변 완료

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

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


안녕하세요, 5주차 숙제 중 질문입니다.

숙제:음식 타입별, 연령별 주문건수 pivot view 만들어 봅시다. (연령은 10~59세 사이)


customers 표에서 age 를 10살부터 59살까지로 추출하고,

10대 20대 .. 50대 연령대 별로 몇 명인지 데이터로 뽑으려면

꼭 case when 절을 써서 원래 나이를 10, 20, .. 50으로 바꿔주어야하나요?


case when 절을 쓰지 않고 group by로 나이를 묶어줄 때는

어떤 기준으로 나이가 group by 되는 걸까요?


case when 절의 유무에 따라 어떤 차이가 있는지 궁금합니다







작성한 코드 및 에러 메세지

제가 쓴 서브쿼리:

<select f.cuisine_type,

c.age,

count(1) cnt_order

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

where c.age between 10 and 59

group by 1, 2

order by 1>


정답 서브쿼리:

<select f.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) cnt_order

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

where c.age between 10 and 59

group by 1, 2

order by 1>오류 발생 시, 작성한 코드 전체와 에러 메시지를 첨부해 주세요.

Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.

Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!




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