Last updated
Calculate yearly sales amount for each product when sales periods may start and end across different years.
Calculate yearly sales amount for each product when sales periods may start and end across different years.
Your dataset contains two sales analysis tables: 1. A Product table with product IDs and product names 2. A Sales table with product sales periods and average daily sales amount Prepare a yearly sales report for all products between 2018 and 2020 based on the number of applicable sales days.
| Column | Type | Key / Notes |
|---|---|---|
| product_id | INT | PK |
| product_name | VARCHAR(255) |
| Column | Type | Key / Notes |
|---|---|---|
| product_id | INT | PK, FK |
| period_start | DATE | |
| period_end | DATE | |
| average_daily_sales | INT |
Product — 5 rows
| product_id | product_name |
|---|---|
| 1 | Mouse |
| 2 | Keyboard |
| 3 | Ram |
| 4 | Processor |
| 5 | Graphic Card |
Sales — 10 rows
| product_id | period_start | period_end | average_daily_sales |
|---|---|---|---|
| 1 | 2018-11-15 | 2019-01-15 | 120 |
| 2 | 2019-03-01 | 2019-03-31 | 80 |
| 5 | 2020-01-01 | 2020-12-31 | 5 |
| 4 | 2018-07-01 | 2018-12-31 | 70 |
| 3 | 2019-06-15 | 2020-01-15 | 120 |
| 1 | 2020-02-01 | 2020-06-30 | 120 |
| 2 | 2020-07-01 | 2020-12-31 | 80 |
| 3 | 2019-08-01 | 2019-12-31 | 45 |
| 4 | 2019-01-01 | 2019-06-30 | 25 |
| 5 | 2018-10-01 | 2019-03-31 | 15 |
Yearly Product Sales Report: What is the total sales amount of each product for each year based on the number of applicable sales days? The sales years are between 2018 and 2020. The final result table should have product_id, product_name, report_year, and total_amount columns.
Your output should look like this (partial example):
| product_id | product_name | report_year | total_amount |
|---|---|---|---|
| 1 | Mouse | 2018 | 5640 |
| 1 | Mouse | 2019 | 1800 |
| 1 | Mouse | 2020 | 18120 |