Compute each employee's 3-month cumulative salary, excluding their most recent month
Company: Amazon
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `Employee` table has one row for each month an employee worked, with the salary paid that month. For each employee and each month they worked, return the 3-month moving sum (that month plus the previous two months) as `id`, `month`, `Salary`. Months the employee did not work count as `0`. Exclude each employee's most recent month, and order by `id` ascending, then `month` descending.
```hint Watch the gaps
The previous two months are `month - 1` and `month - 2` by number. Check what your approach adds up when one of those months is missing for the employee.
```
### Input Table(s)
Table `Employee`
| Column | Type | Meaning |
|---|---|---|
| `id` | INT | Employee id |
| `month` | INT | Month number: 1 for January, 2 for February, and so on |
| `salary` | INT | Salary paid to the employee in that month |
Key: `(id, month)` is unique. An employee has no row for a month they did not work.
### Output Contract
- Columns, in this order and with this exact spelling: `id`, `month`, `Salary`.
- One row for every `Employee` row except the row holding each employee's most recent month.
- For a row with month `m`, `Salary` is the employee's salary in month `m` plus the salaries in months `m - 1` and `m - 2`, where a month with no row counts as 0. It is an integer, with no rounding.
- Rows ordered by `id` ascending, then `month` descending. `(id, month)` is unique, so the order is fully determined.
### Example
Input `Employee`:
| id | month | salary |
|---|---|---|
| 1 | 1 | 100 |
| 1 | 2 | 200 |
| 1 | 4 | 300 |
| 1 | 5 | 400 |
| 1 | 7 | 500 |
| 2 | 3 | 50 |
| 2 | 6 | 70 |
| 3 | 2 | 80 |
Expected output:
| id | month | Salary |
|---|---|---|
| 1 | 5 | 700 |
| 1 | 4 | 500 |
| 1 | 2 | 300 |
| 1 | 1 | 100 |
| 2 | 3 | 50 |
Employee 1's most recent month, 7, is left out. Month 5 adds months 5 and 4 (400 + 300) because month 3 is missing, and month 4 adds months 4 and 2 (300 + 200) for the same reason. Employee 2 keeps only month 3, which has no earlier months. Employee 3 worked a single month, which is also the most recent, so it produces no rows.
### Constraints and Clarifications
- Each `(id, month)` pair is unique.
- `month` is an integer (1 for January, 2 for February, etc.).
- `Employee` has at most 10,000 rows.
- No column is `NULL`.
- There is no year column, so the window never wraps into a previous year: month 1 counts only month 1, and month 2 counts months 1 and 2.
- Only months the employee worked get an output row. A missing month adds 0 to later windows but produces no row of its own.
- "Most recent" is per employee: the largest `month` for that `id`.
Overview: Write a SQL query that returns each employee's 3-month moving salary sum for every month worked, counting missing months as zero and leaving out each employee's most recent month. It tests handling gaps in monthly data, choosing between self-joins and window frames, and filtering per group.