본문 바로가기
코딩테스트/SQL

[solvesql][SQLite] 지역별 주문의 특징

by 포뇨j 2023. 12. 6.

https://solvesql.com/problems/characteristics-of-orders/

 

https://solvesql.com/problems/characteristics-of-orders/

 

solvesql.com

 

select a.region as Region, count(distinct a.order_id) as 'Furniture',
b.'Office Supplies',
c.Technology
from records a
left join
(select region, count(distinct order_id) as 'Office Supplies'
from records
where category = 'Office Supplies'
group by region) b on a.region = b.region
left join
(select region, count(distinct order_id) as Technology
from records
where category = 'Technology'
group by region) c on a.region = c.region
where a.category = 'Furniture'
group by a.region
order by a.region asc

 

Furniture를 count하는 쿼리(a)를 기준으로 Office Supplies(b), Technology(c)를 각각 left join하는 방식으로 풀었다.

 

처음엔 단순한 생각으로 카테고리에서 Furniture, Office Supplies, Technology 개수를 구하도록 count(distinct a.order_id) 부분을 count(a.category)로 작성했다.

그런데 제출해보니 결과 데이터 개수가 맞지 않다는 답을 받았다.

문제를 다시 꼼꼼히 확인한 결과, 주문량을 계산하라고 제시했으며 데이터셋에는 주문 번호 컬럼이 포함되어 있었다.

그렇다면 category를 기준으로 count 한다면 중복되거나 무의미한 데이터가 포함될 수도 있다는 결론을 얻었다.

count(distinct a.order_id)로 수정하여 주문 번호(order_id)를 count한 결과 채점을 통과할 수 있었다.