The situation
The regional dashboard shows the average order value per city. An AI assistant wrote the query in the answer cell, reusing a per-store summary that already existed.
It runs, there is one row per city, and the order counts match the orders table exactly. Then finance divided Mumbai's revenue by Mumbai's order count and got a number that is nowhere near the one on the dashboard.
Find the bug and fix it.
Input tables
orders
One row per order.
| Column | Type | Description |
|---|---|---|
order_id |
BIGINT | Unique per order. |
store_id |
BIGINT | The store that took the order. |
city |
STRING | The city of the store. A store is in one city. |
amount |
DECIMAL(10,2) | The order value. Never NULL. |
Requirement
Return one row per city.
ordersis the number of orders in the city.avg_order_valueis the city's total order amount divided by the city's number of orders, rounded to 2 decimals.
Expected output
One row per city.
| Column | Type | Description |
|---|---|---|
city |
STRING | |
orders |
BIGINT | |
avg_order_value |
DECIMAL(12,2) | Total amount / number of orders, rounded to 2 decimals. |
The AI-written code
-- Average order value per city, built from the per-store statistics WITH store_stats AS ( SELECT city, store_id, COUNT(*) AS orders, AVG(amount) AS avg_amount FROM orders GROUP BY city, store_id ) SELECT city, SUM(orders) AS orders, CAST(ROUND(AVG(avg_amount), 2) AS DECIMAL(12, 2)) AS avg_order_value FROM store_stats GROUP BY city
Explanation
Show explanationGives the answer away. Try the problem first.
The bug
WITH store_stats AS (
SELECT city, store_id, COUNT(*) AS orders, AVG(amount) AS avg_amount
FROM orders
GROUP BY city, store_id
)
SELECT city, SUM(orders) AS orders, AVG(avg_amount) AS avg_order_value
FROM store_stats
GROUP BY city
The first step works out an average per store. The second step averages those averages. That gives every store the same weight, however many orders it has.
Mumbai has four stores:
| Store | Orders | Average order |
|---|---|---|
| 101 | 60 | 2405.10 |
| 102 | 9 | 440.17 |
| 103 | 6 | 304.83 |
| 104 | 4 | 326.50 |
The average of the four averages is 869.15. But 60 of Mumbai's 79 orders were taken by store 101, and the real average order in Mumbai is 1916.49. The four orders of store 104 pull as hard as the sixty of store 101.
| City | Stores | Correct | The buggy query |
|---|---|---|---|
| Chennai | 2 | 1348.23 | 988.57 |
| Delhi | 3 | 908.03 | 1410.43 |
| Indore | 2 | 1282.77 | 1282.77 |
| Kochi | 1 | 966.00 | 966.00 |
| Mumbai | 4 | 1916.49 | 869.15 |
| Pune | 3 | 1559.01 | 868.11 |
The error has no fixed direction. Delhi comes out too high, because there the small store has the large baskets.
Why it got through:
ordersis right in every row. Counts can be added up, soSUM(orders)over the store rows is correct, and that was the column people checked.- Kochi has one store. The average of one average is that average.
- Indore has two stores with 12 orders each. When every group has the same number of rows, the average of the averages equals the overall average. Any test city with evenly sized stores passes.
The fix
Average the orders, not the stores:
SELECT
city,
COUNT(*) AS orders,
CAST(ROUND(AVG(amount), 2) AS DECIMAL(12, 2)) AS avg_order_value
FROM orders
GROUP BY city
An average is a sum divided by a count. If you must build it from a summary table, carry the sum and the count up, and divide at the end:
SELECT city, SUM(orders) AS orders, SUM(total_amount) / SUM(orders) AS avg_order_value
where the store step has SUM(amount) AS total_amount instead of the average. Sums
and counts can be added. Averages cannot.
Common wrong answers
Weighting the store averages: SUM(avg_amount * orders) / SUM(orders). This is
the right idea, and with the unrounded store averages it passes here. It is still
the long way round: it multiplies back what the first step divided, and it depends
on the stored average having enough decimals. If the summary table holds averages
rounded to 2 decimals, which is what a report table usually holds, Delhi comes out
as 908.02 instead of 908.03. Carry the sum instead.
ROUND(AVG(amount), 2) with no cast. The values are right. Spark types the
result as DECIMAL(11,2), the output column is DECIMAL(12,2), and the check
reports the type. The buggy query already has the cast; keep it.
The same mistake in other clothes
- Averaging daily conversion rates to get the monthly rate.
- Averaging percentages from groups of different sizes.
- Averaging the per-city numbers above to get a company number. The average order across all 277 orders is 1409.62. The average of the six city averages is 1330.09, and the average of the fifteen store averages is 1054.73. Three "average order values" for the same data, and only the first is one.
A ratio has to be recomputed from its numerator and denominator at every level it is reported at.
Edge cases in the data
| Case | Where | What it shows |
|---|---|---|
| One big store and several small ones | Mumbai, Pune, Chennai | The average of averages is far too low. |
| A small store with large orders | Delhi, store 121 | The error can also be upwards. |
| A single store | Kochi | The bug is invisible. |
| Two stores with the same number of orders | Indore | The bug is invisible again, for a different reason. |