Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [3421. Find Students Who Improved](./leetcode/medium/3421.%20Find%20Students%20Who%20Improved.sql)
- [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql)
- [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql)
- [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql)
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
3. [Hard](./leetcode/hard/)
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
Expand Down
76 changes: 76 additions & 0 deletions leetcode/medium/3564. Seasonal Sales Analysis.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Question 3564. Seasonal Sales Analysis
Link: https://leetcode.com/problems/seasonal-sales-analysis/description/?envType=problem-list-v2&envId=database

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.
*/

WITH stats AS (
SELECT
p.category,
(CASE
WHEN EXTRACT(MONTH FROM s.sale_date) BETWEEN 3 AND 5
THEN 'Spring'
WHEN EXTRACT(MONTH FROM s.sale_date) BETWEEN 6 AND 8
THEN 'Summer'
WHEN EXTRACT(MONTH FROM s.sale_date) BETWEEN 9 AND 11
THEN 'Fall'
ELSE 'Winter'
END) AS season,
SUM(s.quantity) AS total_quantity,
SUM(s.quantity * s.price) AS total_revenue
FROM sales AS s
LEFT JOIN
products AS p
ON s.product_id = p.product_id
GROUP BY s.season, p.category
)

SELECT
s.season,
s.category,
s.total_quantity,
s.total_revenue
FROM (
SELECT
s1.season,
s1.category,
s1.total_quantity,
s1.total_revenue,
RANK() OVER(PARTITION BY s1.season ORDER BY s1.total_quantity DESC, s1.total_revenue DESC) AS rank
FROM stats AS s1
) AS s
WHERE s.rank = 1
ORDER BY s.season ASC