ROWS vs RANGE Window Frames on Duplicate Dates
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `orders` table has one row per order with its date and amount, and several orders can share a date. Compute two running totals of `amount` in date order across the whole table. Both use a frame from `UNBOUNDED PRECEDING` to `CURRENT ROW`, but one counts the frame in `ROWS` and the other in `RANGE`. The two columns differ only on orders that share a date, and the output should show that difference.
```hint Which row comes first
Within one date, something has to decide the order in which tied rows are added. Check whether your window ordering decides it, and what `CURRENT ROW` means for each frame unit when rows tie.
```
### Input Table(s)
Table `orders`, primary key `order_id`:
| Column | Type | Meaning |
|---|---|---|
| `order_id` | INT | Primary key, one row per order |
| `order_date` | DATE | Date of the order, NOT NULL, not unique |
| `amount` | DECIMAL(10,2) | Order amount, NOT NULL, may be negative |
### Output Contract
Return one row per order with these columns, in this order:
| Column | Meaning |
|---|---|
| `order_id` | The order |
| `order_date` | Date of the order |
| `amount` | Order amount |
| `rows_running` | `SUM(amount)` with `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, window ordered by `order_date`, then `order_id`. Orders on the same date get successive partial sums. |
| `range_running` | `SUM(amount)` with `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, window ordered by `order_date` only. Orders on the same date are peers and all get the total through the end of that date. |
- There is no partitioning: each running total covers the whole table.
- Order the rows by `order_date`, then `order_id`, both ascending. `order_id` is unique, so this order is unique.
### Example
Input `orders`:
| order_id | order_date | amount |
|---|---|---|
| 1 | 2024-01-01 | 100.00 |
| 2 | 2024-01-02 | 50.00 |
| 3 | 2024-01-02 | 30.00 |
| 4 | 2024-01-03 | 20.00 |
Expected output:
| order_id | order_date | amount | rows_running | range_running |
|---|---|---|---|---|
| 1 | 2024-01-01 | 100.00 | 100.00 | 100.00 |
| 2 | 2024-01-02 | 50.00 | 150.00 | 180.00 |
| 3 | 2024-01-02 | 30.00 | 180.00 | 180.00 |
| 4 | 2024-01-03 | 20.00 | 200.00 | 200.00 |
Orders 2 and 3 share 2024-01-02. The ROWS total adds them one at a time in `order_id` order (100 + 50 = 150, then 150 + 30 = 180). The RANGE total treats both as peers and gives each the total through the end of the date, 180. On 2024-01-03 both columns are back at 200.
### Constraints and Clarifications
- The table has 1 to 10,000 rows.
- No column contains NULL.
- `amount` may be negative, so either running total can go down.
- Any number of orders can share an `order_date`. Among them, `order_id` ascending decides the order in which `rows_running` adds them.
- On the last order of each date (by `order_id`), `rows_running` and `range_running` are equal.
Overview: Write a SQL query that computes two running totals of order amounts, one with a ROWS frame and one with a RANGE frame, on data where several orders share a date. It tests window frame semantics, peer rows, the default frame and deterministic ordering of ties.