난이도 3
select search.Region
, sum(case when category = 'Furniture' then search.orderId end) as Furniture
, sum(case when category = 'Office Supplies' then search.orderId end) as 'Office Supplies'
, sum(case when category = 'Technology' then search.orderId end) as Technology
from
(select region AS 'Region'
, category
, count(distinct order_id) orderId
from records
group by region, category
) as search
GROUP BY Region
order by Region;
리뷰
- 한번의 select문으로 해결하려다가 좀 헤맸지만 결국 테이블을 가공하여 from으로 가공된 테이블을 기반으로 사용했다.
- case when ~ then ~ end문을 사용하여 조건이 맞을 경우만 (중복X) 주문 번호를 카운트를 누적 더하기 해주었다.
- 내 기준 여기서 중요한 점은 region, cateogory 별로 그룹화한 것을 다시 region으로 그룹화함으로써 좁혀줬다는 것이다
