Total Sales Amount by Year - Problem

You're working as a data analyst for a major e-commerce company that tracks product sales across multiple years. The company needs a comprehensive yearly sales report to understand product performance trends.

The Challenge: Given two tables - Product (containing product information) and Sales (containing sales periods with average daily sales), you need to calculate the total sales amount for each product for each year from 2018 to 2020.

Key Complexity: Sales periods can span across multiple years! For example, a sales period from December 2018 to February 2019 needs to be split and calculated separately for each year.

Your Task: Write a SQL query that breaks down sales periods by year, calculates the total sales for each product in each year, and returns results ordered by product_id and report_year.

Think of it as creating annual financial reports where you need to allocate revenue to the correct fiscal years, even when sales campaigns cross year boundaries.

Input & Output

example_1.sql โ€” Basic Example
$ Input: Product table:\n| product_id | product_name |\n|------------|-------------|\n| 1 | LC Phone |\n\nSales table:\n| product_id | period_start | period_end | average_daily_sales |\n|------------|-------------|------------|-------------------|\n| 1 | 2019-01-25 | 2019-02-28 | 100 |
โ€บ Output: | product_id | product_name | report_year | total_amount |\n|------------|-------------|-------------|-------------|\n| 1 | LC Phone | 2019 | 3500 |
๐Ÿ’ก Note: Sales period is entirely within 2019. Days from Jan 25 to Feb 28 = 35 days (including both dates). Total = 35 ร— 100 = 3500.
example_2.sql โ€” Cross-Year Sales
$ Input: Product table:\n| product_id | product_name |\n|------------|-------------|\n| 1 | LC Phone |\n\nSales table:\n| product_id | period_start | period_end | average_daily_sales |\n|------------|-------------|------------|-------------------|\n| 1 | 2018-12-15 | 2019-01-15 | 200 |
โ€บ Output: | product_id | product_name | report_year | total_amount |\n|------------|-------------|-------------|-------------|\n| 1 | LC Phone | 2018 | 3400 |\n| 1 | LC Phone | 2019 | 3000 |
๐Ÿ’ก Note: Sales period spans two years. 2018: Dec 15-31 = 17 days ร— 200 = 3400. 2019: Jan 1-15 = 15 days ร— 200 = 3000.
example_3.sql โ€” Multiple Products and Periods
$ Input: Product table:\n| product_id | product_name |\n|------------|-------------|\n| 1 | LC Phone |\n| 2 | LC T-Shirt |\n\nSales table:\n| product_id | period_start | period_end | average_daily_sales |\n|------------|-------------|------------|-------------------|\n| 1 | 2019-01-01 | 2019-12-31 | 50 |\n| 2 | 2018-12-01 | 2020-01-31 | 10 |
โ€บ Output: | product_id | product_name | report_year | total_amount |\n|------------|-------------|-------------|-------------|\n| 1 | LC Phone | 2019 | 18250 |\n| 2 | LC T-Shirt | 2018 | 310 |\n| 2 | LC T-Shirt | 2019 | 3650 |\n| 2 | LC T-Shirt | 2020 | 310 |
๐Ÿ’ก Note: Product 1 sells all of 2019 (365 days ร— 50 = 18250). Product 2 spans multiple years: 2018 (31 days ร— 10 = 310), 2019 (365 days ร— 10 = 3650), 2020 (31 days ร— 10 = 310).

Constraints

  • product_id ranges from 1 to 1000
  • Sales periods are between 2018-01-01 and 2020-12-31
  • average_daily_sales is a positive integer โ‰ค 104
  • Each product can have multiple sales periods
  • Sales periods can overlap or span multiple years

Visualization

Tap to expand
Sales Period: Dec 15, 2018 - Feb 15, 2019Dec 31, 20182018: 17 days2019: 46 daysRevenue Allocation by Year2018: 17 ร— $200 = $3,4002019: 46 ร— $200 = $9,200
Understanding the Visualization
1
Sales Timeline
A sales period from Dec 2018 to Feb 2019
2
Year Boundaries
Mark the year boundaries (Dec 31, 2018)
3
Calculate Overlap
Find intersection: Dec 2018 portion and Jan-Feb 2019 portion
4
Allocate Revenue
Multiply overlap days by daily sales rate for each year
Key Takeaway
๐ŸŽฏ Key Insight: The problem is fundamentally about date range intersections - finding overlap between sales periods and calendar years, then allocating revenue proportionally.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
43.7K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen