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
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