[SQL 문제 풀이] Seasonal Sales Analysis (계절별 매출 분석)

Stupefyee's avatar
May 28, 2025
[SQL 문제 풀이] Seasonal Sales Analysis (계절별 매출 분석)
Seasonal Sales Analysis - LeetCode
Can you solve this real interview question? Seasonal Sales Analysis - Table: sales +---------------+---------+ | Column Name | Type | +---------------+---------+ | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | +---------------+---------+ sale_id is the unique identifier for this table. Each row contains information about a product sale including the product_id, date of sale, quantity sold, and price per unit. Table: products +---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | product_name | varchar | | category | varchar | +---------------+---------+ product_id is the unique identifier for this table. Each row contains information about a product including its name and category. Write a solution to find the most popular product category for each season. The seasons are defined as: * Winter: December, January, February * Spring: March, April, May * Summer: June, July, August * Fall: September, October, November The popularity of a category is determined by the total quantity sold in that season. If there is a tie, select the category with the highest total revenue (quantity × price). Return the result table ordered by season in ascending order. The result format is in the following example.   Example: Input: sales table: +---------+------------+------------+----------+-------+ | sale_id | product_id | sale_date | quantity | price | +---------+------------+------------+----------+-------+ | 1 | 1 | 2023-01-15 | 5 | 10.00 | | 2 | 2 | 2023-01-20 | 4 | 15.00 | | 3 | 3 | 2023-03-10 | 3 | 18.00 | | 4 | 4 | 2023-04-05 | 1 | 20.00 | | 5 | 1 | 2023-05-20 | 2 | 10.00 | | 6 | 2 | 2023-06-12 | 4 | 15.00 | | 7 | 5 | 2023-06-15 | 5 | 12.00 | | 8 | 3 | 2023-07-24 | 2 | 18.00 | | 9 | 4 | 2023-08-01 | 5 | 20.00 | | 10 | 5 | 2023-09-03 | 3 | 12.00 | | 11 | 1 | 2023-09-25 | 6 | 10.00 | | 12 | 2 | 2023-11-10 | 4 | 15.00 | | 13 | 3 | 2023-12-05 | 6 | 18.00 | | 14 | 4 | 2023-12-22 | 3 | 20.00 | | 15 | 5 | 2024-02-14 | 2 | 12.00 | +---------+------------+------------+----------+-------+ products table: +------------+-----------------+----------+ | product_id | product_name | category | +------------+-----------------+----------+ | 1 | Warm Jacket | Apparel | | 2 | Designer Jeans | Apparel | | 3 | Cutting Board | Kitchen | | 4 | Smart Speaker | Tech | | 5 | Yoga Mat | Fitness | +------------+-----------------+----------+ Output: +---------+----------+----------------+---------------+ | season | category | total_quantity | total_revenue | +---------+----------+----------------+---------------+ | Fall | Apparel | 10 | 120.00 | | Spring | Kitchen | 3 | 54.00 | | Summer | Tech | 5 | 100.00 | | Winter | Apparel | 9 | 110.00 | +---------+----------+----------------+---------------+ Explanation: * Fall (Sep, Oct, Nov): * Apparel: 10 items sold (6 Jackets in Sep, 4 Jeans in Nov), revenue $120.00 (6×$10.00 + 4×$15.00) * Fitness: 3 Yoga Mats sold in Sep, revenue $36.00 * Most popular: Apparel with highest total quantity (10) * Spring (Mar, Apr, May): * Kitchen: 3 Cutting Boards sold in Mar, revenue $54.00 * Tech: 1 Smart Speaker sold in Apr, revenue $20.00 * Apparel: 2 Warm Jackets sold in May, revenue $20.00 * Most popular: Kitchen with highest total quantity (3) and highest revenue ($54.00) * Summer (Jun, Jul, Aug): * Apparel: 4 Designer Jeans sold in Jun, revenue $60.00 * Fitness: 5 Yoga Mats sold in Jun, revenue $60.00 * Kitchen: 2 Cutting Boards sold in Jul, revenue $36.00 * Tech: 5 Smart Speakers sold in Aug, revenue $100.00 * Most popular: Tech and Fitness both have 5 items, but Tech has higher revenue ($100.00 vs $60.00) * Winter (Dec, Jan, Feb): * Apparel: 9 items sold (5 Jackets in Jan, 4 Jeans in Jan), revenue $110.00 * Kitchen: 6 Cutting Boards sold in Dec, revenue $108.00 * Tech: 3 Smart Speakers sold in Dec, revenue $60.00 * Fitness: 2 Yoga Mats sold in Feb, revenue $24.00 * Most popular: Apparel with highest total quantity (9) and highest revenue ($110.00) The result table is ordered by season in ascending order.
Seasonal Sales Analysis - LeetCode
notion image
notion image
각 시즌에 가장 인기 있는 제품 카테고리를 찾기 위한 솔루션을 작성하세요. 시즌은 다음과 같이 정의됩니다: * 겨울: 12월, 1월, 2월 * 봄: 3월, 4월, 5월 * 여름: 6월, 7월, 8월 * 가을: 9월, 10월, 11월 카테고리의 인기는 해당 시즌에 판매된 총 수량에 따라 결정됩니다. 동점일 경우 총 수익이 가장 높은 카테고리(수량 × 가격)를 선택합니다. 계절별로 정렬된 결과 표를 오름차순으로 반환합니다.
 

내가 작성한 쿼리

Oracle

WITH sales_with_season AS ( SELECT s.*, p.category, CASE WHEN EXTRACT(MONTH FROM s.sale_date) IN (12, 1, 2) THEN 'Winter' WHEN EXTRACT(MONTH FROM s.sale_date) IN (3, 4, 5) THEN 'Spring' WHEN EXTRACT(MONTH FROM s.sale_date) IN (6, 7, 8) THEN 'Summer' ELSE 'Fall' END AS season FROM sales s JOIN products p ON s.product_id = p.product_id ), category_stats AS ( SELECT season, category, SUM(quantity) AS total_quantity, SUM(quantity * price) AS total_revenue FROM sales_with_season GROUP BY season, category ), ranked_categories AS ( SELECT category_stats.*, RANK() OVER ( PARTITION BY season ORDER BY total_quantity DESC, total_revenue DESC ) AS rk FROM category_stats ) SELECT season, category, total_quantity, ROUND(total_revenue, 2) AS total_revenue FROM ranked_categories WHERE rk = 1 ORDER BY season;

MySQL

-- 1단계: 각 판매 내역에 계절(season)과 카테고리(category) 컬럼 추가 WITH sales_with_season AS ( SELECT s.*, -- 판매 테이블의 모든 컬럼 p.category, -- 상품 카테고리 CASE -- 판매 월에 따라 계절 분류 WHEN MONTH(s.sale_date) IN (12, 1, 2) THEN 'Winter' WHEN MONTH(s.sale_date) IN (3, 4, 5) THEN 'Spring' WHEN MONTH(s.sale_date) IN (6, 7, 8) THEN 'Summer' ELSE 'Fall' -- 9,10,11월은 Fall END AS season FROM sales s JOIN products p ON s.product_id = p.product_id -- 상품 ID 기준으로 products와 조인 ), -- 2단계: 계절과 카테고리별로 총 수량, 총 매출을 집계 category_stats AS ( SELECT season, category, SUM(quantity) AS total_quantity, -- 총 판매 수량 SUM(quantity * price) AS total_revenue -- 총 매출액 FROM sales_with_season GROUP BY season, category ), -- 3단계: 계절별로 카테고리에 순위 부여 ranked_categories AS ( SELECT *, RANK() OVER ( PARTITION BY season -- 계절별로 나누고 ORDER BY total_quantity DESC, -- 수량 높은 순 total_revenue DESC -- 수량이 같다면 매출 높은 순 ) AS rank FROM category_stats ) -- 4단계: 순위 1위인 카테고리만 선택하여 출력 SELECT season, category, total_quantity, ROUND(total_revenue, 2) AS total_revenue -- 매출은 소수 둘째자리까지 반올림 FROM ranked_categories WHERE rank = 1 -- 1등만 선택 ORDER BY season; -- 계절 순으로 정렬

차이점

  • 월(Month) 추출 방식의 차이
    • Oracle: EXTRACT(추출요소(MONTH) FROM 추출할 컬럼)
    • MySQL: MONTH(추출할 컬럼)
Share article

stupefyee