Per-channel order metrics in one pass with conditional aggregation
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `orders` table has one row per order with its sales channel, status and amount. Write a single query that computes the metrics below for each `channel` in one pass over `orders`, using conditional aggregation (a `CASE` expression inside each aggregate), so the table is not scanned once per metric.
- `total_orders`: the number of orders in the channel.
- `completed_orders`: orders with `status = 'completed'`.
- `cancelled_orders`: orders with `status = 'cancelled'`.
- `completed_revenue`: the sum of `amount` over completed orders, rounded to 2 decimals. It is 0, never NULL, when the channel has no completed orders.
- `avg_completed_order_value`: `completed_revenue / completed_orders`, rounded to 2 decimals. It is NULL when the channel has no completed orders.
- `completion_rate_pct`: `completed_orders / total_orders * 100`, rounded to 2 decimals.
A NULL `amount` contributes 0 to the sums.
```hint Watch the division
In PostgreSQL, an integer divided by an integer truncates. Check the types going into the completion rate.
```
### Input Table(s)
`orders`
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order id |
| channel | VARCHAR(20) | Sales channel, such as `web` or `app` |
| status | VARCHAR(20) | `'completed'`, `'cancelled'` or `'pending'` |
| amount | DECIMAL(10,2) | Order amount, can be NULL |
Primary key: `order_id`.
### Output Contract
One row per channel, with these columns in this order:
| Column | Meaning |
|---|---|
| channel | The channel |
| total_orders | Number of orders |
| completed_orders | Number of completed orders |
| cancelled_orders | Number of cancelled orders |
| completed_revenue | Sum of completed amounts, rounded to 2 decimals, 0 when there are none |
| avg_completed_order_value | `completed_revenue / completed_orders`, rounded to 2 decimals, NULL when there are no completed orders |
| completion_rate_pct | `completed_orders / total_orders * 100`, rounded to 2 decimals |
Order the rows by `channel` ascending.
### Example
`orders`
| order_id | channel | status | amount |
|---|---|---|---|
| 1 | web | completed | 100.00 |
| 2 | web | cancelled | 50.00 |
| 3 | web | completed | 200.00 |
| 4 | app | pending | 30.00 |
| 5 | app | completed | 60.00 |
Expected result:
| channel | total_orders | completed_orders | cancelled_orders | completed_revenue | avg_completed_order_value | completion_rate_pct |
|---|---|---|---|---|---|---|
| app | 2 | 1 | 0 | 60.00 | 60.00 | 50.00 |
| web | 3 | 2 | 1 | 300.00 | 150.00 | 66.67 |
`app` has one completed order for 60.00 and one pending order, so its rate is 1 / 2 * 100 = 50.00. `web` has two completed orders worth 100 + 200 = 300.00 (an average of 150.00) and one cancelled order, so its rate is 2 / 3 * 100 = 66.67 after rounding.
### Constraints and Clarifications
- `orders` has 1 to 10,000 rows.
- `status` is always one of `'completed'`, `'cancelled'` and `'pending'`. Pending orders count toward `total_orders` only.
- `amount` can be NULL. A completed order with a NULL amount adds 0 to `completed_revenue` but still counts in `completed_orders`, so it lowers the average.
- Every channel in the output has at least one order, so `total_orders` is never 0.
- Each channel appears once in the output. If `channel` is NULL on some rows, those rows form their own group, which sorts last.
Overview: Write one SQL query that reports order counts, completed revenue, average completed order value and completion rate for each sales channel in a single pass. It tests conditional aggregation with CASE, NULL handling in sums, and safe division.