Total Sales Amount By Year Problem
Description
LeetCode Problem 1384.
Table: Product
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| product_name | varchar |
+---------------+---------+
product_id is the primary key for this table.
product_name is the name of the product.
Table: Sales
+---------------------+---------+
| Column Name | Type |
+---------------------+---------+
| product_id | int |
| period_start | date |
| period_end | date |
| average_daily_sales | int |
+---------------------+---------+
product_id is the primary key for this table.
period_start and period_end indicates the start and end date for sales period, both dates are inclusive.
The average_daily_sales column holds the average daily sales amount of the items for the period.
Write an SQL query to report the Total sales amount of each item for each year, with corresponding product name, product_id, product_name and report_year.
Dates of the sales years are between 2018 to 2020. Return the result table ordered by product_id and report_year.
The query result format is in the following example:
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 1 | LC Phone |
| 2 | LC T-Shirt |
| 3 | LC Keychain |
+------------+--------------+
Sales table:
+------------+--------------+-------------+---------------------+
| product_id | period_start | period_end | average_daily_sales |
+------------+--------------+-------------+---------------------+
| 1 | 2019-01-25 | 2019-02-28 | 100 |
| 2 | 2018-12-01 | 2020-01-01 | 10 |
| 3 | 2019-12-01 | 2020-01-31 | 1 |
+------------+--------------+-------------+---------------------+
Result table:
+------------+--------------+-------------+--------------+
| product_id | product_name | report_year | total_amount |
+------------+--------------+-------------+--------------+
| 1 | LC Phone | 2019 | 3500 |
| 2 | LC T-Shirt | 2018 | 310 |
| 2 | LC T-Shirt | 2019 | 3650 |
| 2 | LC T-Shirt | 2020 | 10 |
| 3 | LC Keychain | 2019 | 31 |
| 3 | LC Keychain | 2020 | 31 |
+------------+--------------+-------------+--------------+
LC Phone was sold for the period of 2019-01-25 to 2019-02-28, and there are 35 days for this period. Total amount 35*100 = 3500.
LC T-shirt was sold for the period of 2018-12-01 to 2020-01-01, and there are 31, 365, 1 days for years 2018, 2019 and 2020 respectively.
LC Keychain was sold for the period of 2019-12-01 to 2020-01-31, and there are 31, 31 days for years 2019 and 2020 respectively.
Solution
Approach: Join Each Period to the Years It Overlaps
A sales period can cross year boundaries (LC T-Shirt runs from 2018-12-01 to 2020-01-01), so each period has to be split by year.
- A small year table. List 2018, 2019 and 2020 with each year's first and last day.
- Pair periods with years. Join every sales period to the years it overlaps: the period starts on or before the year's last day and ends on or after its first day.
- Count the days inside the year. The overlap runs from
GREATEST(period_start, year_start)toLEAST(period_end, year_end). Both ends are inclusive, so the number of days isDATEDIFF(end, start) + 1. Multiply byaverage_daily_sales.
Join Product for the name, and order by product_id and report_year. report_year is cast to text with CAST(... AS CHAR). Because the day count comes from real dates, leap years are handled too: 2020 has 366 days.
The Pandas version cross-merges the sales with the three years, clips each period to the year with row-wise max/min, keeps the pairs with a positive number of days, and multiplies.
WITH years AS (
SELECT 2018 AS report_year, DATE '2018-01-01' AS year_start, DATE '2018-12-31' AS year_end
UNION ALL SELECT 2019, DATE '2019-01-01', DATE '2019-12-31'
UNION ALL SELECT 2020, DATE '2020-01-01', DATE '2020-12-31'
)
SELECT s.product_id,
p.product_name,
CAST(y.report_year AS CHAR) AS report_year,
-- Days of the sales period that fall inside this year (both ends inclusive).
s.average_daily_sales
* (DATEDIFF(LEAST(s.period_end, y.year_end), GREATEST(s.period_start, y.year_start)) + 1) AS total_amount
FROM Sales AS s
JOIN Product AS p
ON p.product_id = s.product_id
JOIN years AS y
ON s.period_start <= y.year_end
AND s.period_end >= y.year_start
ORDER BY s.product_id, y.report_year;