Pivot sales into one row per region with a column per category and a total
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Pivot the `sales` table into one row per `region`. Each output row has one column per category (`electronics`, `furniture`, `grocery`) holding the region's total sales amount in that category, plus a `total` column with the region's overall amount.
```hint Watch the empty cells
Check what `SUM` returns when every value it aggregates is `NULL`, and make sure no cell of the result can come out as `NULL`.
```
### Input Table(s)
`sales`
| Column | Type | Meaning |
|---|---|---|
| sale_id | INT | Sale identifier |
| region | VARCHAR(50) | Region where the sale happened, NOT NULL |
| category | VARCHAR(50) | Product category, NOT NULL |
| amount | DECIMAL(10,2) | Sale amount, may be NULL |
Primary key: `sale_id`.
### Output Contract
- Columns, in this order: `region`, `electronics`, `furniture`, `grocery`, `total`.
- One row per distinct `region` that appears in `sales`.
- `electronics`, `furniture` and `grocery` hold the sum of `amount` for that region and category, and `0` when the region has no sales in the category.
- `total` holds the sum of `amount` over all of the region's sales.
- A `NULL` amount contributes `0`, so no output cell is ever `NULL`.
- Round every monetary column to 2 decimal places.
- Order by `region` ascending. `region` is unique in the output, so the order is fully determined.
### Example
Input `sales`:
| sale_id | region | category | amount |
|---|---|---|---|
| 1 | east | electronics | 100.00 |
| 2 | east | furniture | 50.00 |
| 3 | east | electronics | 25.50 |
| 4 | west | grocery | 30.00 |
| 5 | west | electronics | 70.00 |
Expected output:
| region | electronics | furniture | grocery | total |
|---|---|---|---|---|
| east | 125.50 | 50.00 | 0.00 | 175.50 |
| west | 70.00 | 0.00 | 30.00 | 100.00 |
East has electronics 100.00 + 25.50 = 125.50, furniture 50.00, no grocery sales, and a total of 175.50. West has electronics 70.00, no furniture sales, grocery 30.00, and a total of 100.00.
### Constraints and Clarifications
- `sales` has between 1 and 10,000 rows.
- `region` and `category` are non-empty lowercase strings, and `category` is always one of `electronics`, `furniture`, `grocery`.
- `amount` can be `NULL`. It counts as `0` everywhere, including a region whose only sales in a category have `NULL` amounts (that cell is `0.00`) and a region whose every sale has a `NULL` amount (every cell is `0.00`).
- Two sales can share the same region, category and amount. They are separate sales and both count.
- Regions with no rows in `sales` do not appear in the output.
- The result must not depend on the physical order of the rows.
Overview: Pivot a sales table into one row per region, with a column for each of three product categories and an overall total. It tests turning rows into columns with grouped aggregates, handling NULL amounts and categories with no sales, rounding, and stable ordering.