Moving Average of Transaction Volumes Over a 3-Day Calendar Window
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
For each date in the `transactions` table, compute the 3-day moving average of `volume` over the current day and the previous 2 calendar days. Average over whatever rows exist in that window. Dates can have gaps, so a window may hold one, two or three rows.
```hint Gaps in the dates
Work through 2024-03-05 in the example with a window that counts the previous two rows, then with one that counts the previous two calendar days. Only one of them gives 350.00.
```
### Input Table(s)
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_date | DATE | The day the volume was recorded |
| volume | INT | Transaction volume on that day, NOT NULL |
Primary key: `transaction_date` (one row per date).
### Output Contract
Return one row per row of `transactions`, with these columns in this order:
| Column | Type | Meaning |
|---|---|---|
| transaction_date | DATE | The date of the row |
| volume | INT | That day's volume |
| moving_avg | NUMERIC | Average `volume` over all rows whose `transaction_date` is between this date minus 2 days and this date, inclusive, rounded to 2 decimal places |
Order the result by `transaction_date` ascending. The date is the primary key, so the order is unique.
### Example
Input `transactions`:
| transaction_date | volume |
|---|---|
| 2024-03-01 | 500 |
| 2024-03-03 | 300 |
| 2024-03-05 | 400 |
Expected output:
| transaction_date | volume | moving_avg |
|---|---|---|
| 2024-03-01 | 500 | 500.00 |
| 2024-03-03 | 300 | 400.00 |
| 2024-03-05 | 400 | 350.00 |
The dates are not consecutive. For 2024-03-01 the window (2024-02-28 to 2024-03-01) holds only that row, so the average is 500.00. For 2024-03-03 the window (2024-03-01 to 2024-03-03) holds 500 and 300, so the average is (500 + 300) / 2 = 400.00. For 2024-03-05 the window (2024-03-03 to 2024-03-05) holds 300 and 400; 2024-03-01 is outside it, so the average is (300 + 400) / 2 = 350.00.
### Constraints and Clarifications
- The table has between 1 and 10,000 rows, one per date.
- `volume` is a non-negative integer and is never NULL.
- Dates may have gaps. The window covers calendar days, and a missing day adds nothing to the average. It is not counted as a zero.
- Every window contains at least the current row, so `moving_avg` is never NULL.
- For the earliest dates the window reaches back before the first date in the table. Average over the rows that exist.
Overview: Compute a 3-day moving average of daily transaction volume in SQL when some dates are missing from the table. It tests date-range self-joins or calendar-based window frames, the difference between row-count and calendar windows, and correct rounding of averages.
For each date in the `transactions` table, compute the 3-day moving average of `volume` over the current day and the previous 2 calendar days. Average over whatever rows exist in that window. Dates can have gaps, so a window may hold one, two or three rows.
### Input Table
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_date | DATE | The day the volume was recorded |
| volume | INTEGER | Transaction volume on that day, NOT NULL |
Primary key: `transaction_date` (one row per date).
### Output Contract
Return one row per row of `transactions`, with these columns in this order:
| Column | Type | Meaning |
|---|---|---|
| transaction_date | DATE | The date of the row |
| volume | INTEGER | That day's volume |
| moving_avg | NUMERIC | Average `volume` over all rows whose `transaction_date` is between this date minus 2 days and this date, inclusive, rounded to 2 decimal places |
Order the result by `transaction_date` ascending. The date is the primary key, so the order is unique.
### Example
Input `transactions`:
| transaction_date | volume |
|---|---|
| 2024-03-01 | 500 |
| 2024-03-03 | 300 |
| 2024-03-05 | 400 |
Expected output:
| transaction_date | volume | moving_avg |
|---|---|---|
| 2024-03-01 | 500 | 500.00 |
| 2024-03-03 | 300 | 400.00 |
| 2024-03-05 | 400 | 350.00 |
The dates are not consecutive. For 2024-03-01 the window (2024-02-28 to 2024-03-01) holds only that row, so the average is 500.00. For 2024-03-03 the window (2024-03-01 to 2024-03-03) holds 500 and 300, so the average is (500 + 300) / 2 = 400.00. For 2024-03-05 the window (2024-03-03 to 2024-03-05) holds 300 and 400; 2024-03-01 is outside it, so the average is (300 + 400) / 2 = 350.00.
### Constraints and Clarifications
- The table has between 1 and 10,000 rows, one per date.
- `volume` is a non-negative integer and is never NULL.
- Dates may have gaps. The window covers calendar days, and a missing day adds nothing to the average. It is not counted as a zero.
- Every window contains at least the current row, so `moving_avg` is never NULL.
- For the earliest dates the window reaches back before the first date in the table. Average over the rows that exist.
Tables
transactions(transaction_date DATE, volume INTEGER)
Hints
- Work through 2024-03-05 in the example with a window that counts the previous two rows, then with one that counts the previous two calendar days. Only one of them gives 350.00.
- A day with no row adds nothing to its window, and a row whose volume is 0 still counts toward the average.