Sales report with region subtotals and a grand total using ROLLUP and GROUPING()
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `sales` table has one row per order. Using `ROLLUP`, group the orders by `region` and `product_category` and return, in one result, a detail row for each (region, product category) pair, a subtotal row for each region, and one grand total row.
Use `GROUPING()` to detect which dimensions were rolled up in a row, and show each rolled-up dimension as the literal string `'ALL'`. Report `region`, `product_category`, `total_amount`, `order_count` and `grouping_level`.
```hint Ordering
Sorting by the labeled `region` alone puts the grand total first, because `'ALL'` sorts before `'East'`. Look for a value you can sort on before the region name.
```
### Input Table(s)
`sales`
| Column | Type | Meaning |
|---|---|---|
| id | INT | Order id |
| region | VARCHAR(50) | Region where the order was placed |
| product_category | VARCHAR(50) | Category of the product ordered |
| amount | DECIMAL(10,2) | Order amount |
Primary key: `id`.
### Output Contract
One row per group, with these columns in this order:
| Column | Meaning |
|---|---|
| region | The region, or `'ALL'` on the grand total row |
| product_category | The product category, or `'ALL'` on region subtotal rows and the grand total row |
| total_amount | Sum of `amount` over the group, rounded to 2 decimals |
| order_count | Number of `sales` rows in the group |
| grouping_level | Number of rolled-up dimensions: `0` for a detail row, `1` for a region subtotal, `2` for the grand total |
Order the rows by region ascending. Within a region, list its detail rows by `product_category` ascending, then that region's subtotal row. The grand total row comes last. NULL values stored in the data sort after non-NULL values, which is PostgreSQL's default for ascending order.
### Example
`sales`
| id | region | product_category | amount |
|---|---|---|---|
| 1 | East | Electronics | 100.00 |
| 2 | East | Toys | 50.00 |
| 3 | West | Electronics | 200.00 |
Expected result:
| region | product_category | total_amount | order_count | grouping_level |
|---|---|---|---|---|
| East | Electronics | 100.00 | 1 | 0 |
| East | Toys | 50.00 | 1 | 0 |
| East | ALL | 150.00 | 2 | 1 |
| West | Electronics | 200.00 | 1 | 0 |
| West | ALL | 200.00 | 1 | 1 |
| ALL | ALL | 350.00 | 3 | 2 |
The three detail rows are the orders themselves. East's subtotal is 100 + 50 = 150 over 2 orders, West's is 200 over 1 order, and the grand total is 350 over all 3 orders.
### Constraints and Clarifications
- `sales` has 1 to 10,000 rows.
- `amount` is never NULL, is non-negative, has at most 2 decimal places and is at most 1,000,000 per row.
- `region` and `product_category` are non-empty strings when present, but either one can be NULL. A NULL stored in the data is a real group: it stays NULL in the output (never `'ALL'`) and gets its own detail and subtotal rows.
- Several orders with the same region and category collapse into one detail row, and `order_count` counts all of them.
- No stored region or category equals the string `'ALL'`.
Overview: Write a PostgreSQL query that uses ROLLUP to return sales detail rows per region and product category, a subtotal for each region, and one grand total. It tests GROUPING() for labeling rolled-up rows and an ORDER BY that keeps each subtotal next to its region.