지역별 주문의 특징
난이도3
https://solvesql.com/problems/characteristics-of-orders/
SELECT
region AS Region,
SUM(CASE WHEN category='Furniture' THEN 1 ELSE 0 END) AS Furniture,
SUM(CASE WHEN category='Office Supplies' THEN 1 ELSE 0 END) AS "Office Supplies",
SUM(CASE WHEN category='Technology' THEN 1 ELSE 0 END) AS Technology
FROM records
GROUP BY region
ORDER BY region ASC;
SELECT
region AS Region,
SUM(CASE WHEN category='Furniture' THEN order_count ELSE NULL END) AS 'Furniture',
SUM(CASE WHEN category='Office Supplies' THEN order_count ELSE NULL END) AS 'Office Supplies',
SUM(CASE WHEN category='Technology' THEN order_count ELSE NULL END) AS 'Technology'
FROM (
SELECT
region,
COUNT(DISTINCT(order_id)) AS order_count,
category
FROM records
GROUP BY region, category
)
GROUP BY region
ORDER BY region;
SELECT
region AS Region,
COUNT(DISTINCT(CASE WHEN category='Furniture' THEN order_id ELSE NULL END)) AS 'Furniture',
COUNT(DISTINCT(CASE WHEN category='Office Supplies' THEN order_id ELSE NULL END)) AS 'Office Supplies',
COUNT(DISTINCT(CASE WHEN category='Technology' THEN order_id ELSE NULL END)) AS 'Technology'
FROM records
GROUP BY region
ORDER BY region;