Compute each product's running sales total that resets at every restock
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Compute the running total of quantity sold for each product, resetting the total at each restock of that product. A restock affects sales on or after its date, so sales on the restock date start the new group. A product with no restocks accumulates continuously across all of its sales.
```hint Think in segments
A reset is easier to express as a new group than as arithmetic on the previous total. Look for a value that stays the same for every sale of a product between two of its restocks.
```
### Input Table(s)
Table `sales`
| Column | Type | Meaning |
|---|---|---|
| sale_id | INT | Sale identifier |
| product_id | INT | Product sold (NOT NULL) |
| sale_date | DATE | Date of the sale (NOT NULL) |
| quantity | INT | Units sold in this sale (NOT NULL) |
Keys: `sale_id` is the primary key.
Table `restocks`
| Column | Type | Meaning |
|---|---|---|
| restock_id | INT | Restock identifier |
| product_id | INT | Product that was restocked (NOT NULL) |
| restock_date | DATE | Date of the restock (NOT NULL) |
| restock_quantity | INT | Units added (NOT NULL) |
Keys: `restock_id` is the primary key.
### Output Contract
- Columns, in this order: `sale_id`, `product_id`, `sale_date`, `quantity`, `running_total`.
- One row per row of `sales`.
- `running_total` is the sum of `quantity` over the sales of the same product in the same restock segment, up to and including the current sale.
- Order by `product_id`, `sale_date`, `sale_id`, all ascending.
- All values are integers; no rounding.
### Example
Input `sales`:
| sale_id | product_id | sale_date | quantity |
|---|---|---|---|
| 1 | 10 | 2024-01-01 | 5 |
| 2 | 10 | 2024-01-03 | 3 |
| 3 | 10 | 2024-01-05 | 4 |
| 4 | 10 | 2024-01-05 | 2 |
| 5 | 10 | 2024-01-08 | 6 |
| 6 | 20 | 2024-01-02 | 7 |
| 7 | 20 | 2024-01-04 | 1 |
Input `restocks`:
| restock_id | product_id | restock_date | restock_quantity |
|---|---|---|---|
| 1 | 10 | 2024-01-05 | 50 |
| 2 | 10 | 2024-01-07 | 40 |
Expected output:
| sale_id | product_id | sale_date | quantity | running_total |
|---|---|---|---|---|
| 1 | 10 | 2024-01-01 | 5 | 5 |
| 2 | 10 | 2024-01-03 | 3 | 8 |
| 3 | 10 | 2024-01-05 | 4 | 4 |
| 4 | 10 | 2024-01-05 | 2 | 6 |
| 5 | 10 | 2024-01-08 | 6 | 6 |
| 6 | 20 | 2024-01-02 | 7 | 7 |
| 7 | 20 | 2024-01-04 | 1 | 8 |
Product 10 is restocked on 2024-01-05, so sale 3 on that same date starts a new total at 4, and sale 4 later that day adds 2 for 6. The restock on 2024-01-07 has no sale on its date, but it still resets the total, so sale 5 starts again at 6. Product 20 has no restocks, so its total keeps growing (7, then 8).
### Constraints and Clarifications
- `sales` has at most 10,000 rows and `restocks` at most 1,000 rows.
- `quantity` and `restock_quantity` are positive integers. `restock_quantity` does not enter the total; only the restock date matters.
- No column contains NULLs.
- A product may have zero or more restocks. With none, the total accumulates over all of its sales.
- A product can have several sales on the same date. Within a date, accumulate in `sale_id` order.
- Two restocks of the same product on the same date reset the total once.
- Restocks of one product never affect another product's total.
Overview: Write a SQL query that gives every sale a running total of units sold for its product, starting over each time that product is restocked, with sales on the restock date opening the new segment. It tests window functions, building group labels for resets, and ordering same-day rows.