Build a monthly revenue growth matrix with YoY, MoM and QoQ percent changes per product
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `sales` table holds individual sales, each tagged with the year, quarter and month it belongs to. Roll the sales up to monthly revenue per product, then attach three growth rates to every product-month: year over year, month over month and quarter over quarter.
```hint Gaps in the calendar
A product can skip months. For each metric, decide whether it needs the previous row of the product's monthly sequence or a month a fixed distance back, and use a construct that guarantees that.
```
### Input Table(s)
Table `sales`, primary key `sale_id`:
| Column | Type | Meaning |
|---|---|---|
| `sale_id` | INT | Primary key, one row per sale |
| `product_id` | INT | Product sold, NOT NULL |
| `period_year` | INT | Year of the sale, NOT NULL |
| `period_quarter` | INT | Quarter of the sale (1 to 4), NOT NULL |
| `period_month` | INT | Month of the sale (1 to 12), NOT NULL |
| `amount` | DECIMAL(12,2) | Sale amount, NOT NULL, zero or positive |
### Output Contract
Return one row per `(product_id, period_year, period_month)` that has at least one sale, with these columns in this order:
| Column | Meaning |
|---|---|
| `product_id` | The product |
| `period_year` | Year of the month |
| `period_quarter` | Quarter of the month |
| `period_month` | The month |
| `revenue` | `SUM(amount)` for the product in that month |
| `yoy_pct` | Change against the product's revenue in the same month one year earlier |
| `mom_pct` | Change against the product's previous month with sales, in chronological order. When the product skipped months, this month can be more than one calendar month back. |
| `qoq_pct` | Change against the product's immediately preceding period with sales, with the product's months ordered by year, then quarter, then month |
- Each percent is (current - previous) / previous × 100, rounded to 2 decimals.
- A percent is NULL when there is no comparison row for the product, or when its revenue is 0.
- Order the rows by `product_id`, `period_year`, `period_month`, all ascending. These three columns are unique per output row.
### Example
Input `sales`:
| sale_id | product_id | period_year | period_quarter | period_month | amount |
|---|---|---|---|---|---|
| 1 | 100 | 2022 | 1 | 1 | 1000.00 |
| 2 | 100 | 2022 | 1 | 2 | 1200.00 |
| 3 | 100 | 2023 | 1 | 1 | 1500.00 |
| 4 | 100 | 2023 | 1 | 2 | 1200.00 |
| 5 | 200 | 2023 | 1 | 3 | 300.00 |
| 6 | 200 | 2023 | 1 | 3 | 100.00 |
| 7 | 200 | 2023 | 2 | 4 | 0.00 |
| 8 | 200 | 2023 | 2 | 5 | 250.00 |
| 9 | 200 | 2023 | 2 | 6 | 500.00 |
Expected output:
| product_id | period_year | period_quarter | period_month | revenue | yoy_pct | mom_pct | qoq_pct |
|---|---|---|---|---|---|---|---|
| 100 | 2022 | 1 | 1 | 1000.00 | NULL | NULL | NULL |
| 100 | 2022 | 1 | 2 | 1200.00 | NULL | 20.00 | 20.00 |
| 100 | 2023 | 1 | 1 | 1500.00 | 50.00 | 25.00 | 25.00 |
| 100 | 2023 | 1 | 2 | 1200.00 | 0.00 | -20.00 | -20.00 |
| 200 | 2023 | 1 | 3 | 400.00 | NULL | NULL | NULL |
| 200 | 2023 | 2 | 4 | 0.00 | NULL | -100.00 | -100.00 |
| 200 | 2023 | 2 | 5 | 250.00 | NULL | NULL | NULL |
| 200 | 2023 | 2 | 6 | 500.00 | NULL | 100.00 | 100.00 |
For product 100, January 2023 is compared with January 2022 for YoY (1000 to 1500 is +50.00) and with February 2022, its preceding month with sales, for MoM and QoQ (1200 to 1500 is +25.00). February 2023 is flat against February 2022 (0.00) and down 20.00 against January 2023. January 2022 has no earlier data, so all three rates are NULL.
For product 200, the two March sales add up to 400.00. April drops to 0.00 (-100.00). May's MoM and QoQ are NULL because their baseline, April, is 0. June is up 100.00 on May. Product 200 has no sales in 2022, so every YoY is NULL.
### Constraints and Clarifications
- The table has 1 to 10,000 rows.
- `period_year` is a 4-digit year, and `period_quarter` always matches the month (months 1 to 3 are quarter 1, months 4 to 6 are quarter 2, and so on).
- Several sales in the same product-month are summed into one `revenue` value.
- A month whose sales sum to 0 still produces an output row with revenue 0.00, and any growth rate measured against it is NULL.
- A month with no sales has no row and is never treated as revenue 0.
- YoY compares with the exact same month one year earlier: January 2023 with January 2022. If that month has no row, YoY is NULL even when an older year has one.
Overview: Write a SQL query that rolls individual sales up to monthly revenue per product and adds year-over-year, month-over-month and quarter-over-quarter percent changes. It tests window functions, calendar-offset self-joins, gaps in a monthly series and divide-by-zero handling.