Monthly Product Sales
Start Timer
0:00:00
Given a table containing data for monthly sales, write a query to find the total amount of each product sold for each month with each product as its own column in the output table.
Schema
Input:
monthly_sales table
month |
product_id |
amount_sold |
|---|---|---|
| 2021-01-01 | 1 | 100 |
| 2021-01-01 | 2 | 300 |
| 2021-02-01 | 3 | 200 |
| 2021-03-01 | 4 | 250 |
Output:
| month | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| 2021-01-01 | 100 | 0 | 300 | 0 |
| 2021-02-01 | 0 | 200 | 0 | 0 |
| 2021-03-01 | 0 | 0 | 0 | 250 |
Example
Input:
monthly_sales table
| month | product_id | amount_sold |
|---|---|---|
| 2021-01-01 | 1 | 100 |
| 2021-01-01 | 2 | 0 |
| 2021-01-01 | 3 | 250 |
| 2021-01-01 | 4 | 300 |
| 2021-02-01 | 1 | 50 |
| 2021-02-01 | 2 | 200 |
| 2021-02-01 | 3 | 0 |
| 2021-02-01 | 4 | 300 |
| 2021-03-01 | 1 | 400 |
| 2021-03-01 | 2 | 150 |
| 2021-03-01 | 3 | 200 |
| 2021-03-01 | 4 | 300 |
| 2021-04-01 | 1 | 150 |
| 2021-04-01 | 2 | 200 |
| 2021-04-01 | 3 | 50 |
| 2021-04-01 | 4 | 300 |
| 2021-05-01 | 1 | 100 |
| 2021-05-01 | 2 | 150 |
| 2021-05-01 | 3 | 0 |
| 2021-05-01 | 4 | 300 |
Output:
| month | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| 2021-01-01 | 100 | 0 | 250 | 300 |
| 2021-02-01 | 50 | 200 | 0 | 300 |
| 2021-03-01 | 400 | 150 | 200 | 300 |
| 2021-04-01 | 150 | 200 | 50 | 300 |
| 2021-05-01 | 100 | 150 | 0 | 300 |
.
.
.
.
.
.
.
.
.
Comments