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.
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.
Table `employee`:
| Column | Type | Meaning |
|---|---|---|
| `id` | INTEGER | Employee id |
| `month` | INTEGER | Month number: 1 for January, 2 for February, and so on |
| `salary` | INTEGER | Salary paid to the employee in that month |
Each `(id, month)` pair is unique. An employee has no row for a month they did not work. No column is NULL, and the table has at most 10,000 rows.
Output:
- Columns, in this order: `id`, `month`, `salary`. The original prompt writes the third column as `Salary`; return it under the lower-case name `salary`.
- One row for every `employee` row except the row holding each employee's most recent month. Most recent is per employee: the largest `month` for that `id`.
- 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.
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 and has no output row of its own. An employee who worked a single month gets no rows, because that month is also their most recent one.
For example, if an employee worked months 1, 2, 4, 5 and 7, month 7 is left out, month 5 adds months 5 and 4 because month 3 is missing, month 4 adds months 4 and 2 for the same reason, month 2 adds months 2 and 1, and month 1 counts only itself.
Tables
employee(id INTEGER, month INTEGER, salary INTEGER)
Hints
- 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.
- Each employee's most recent month is that employee's own largest `month`, so the row to leave out can be a different month for each `id`.