Average of ratios vs ratio of sums
It happens whenever a rate is rolled up: a daily conversion rate averaged over the month, a margin per category averaged into a total, a dashboard that stores only percentages. The overall rate is the sum of the numerators divided by the sum of the denominators.
SELECT 100.0 * SUM(orders) / NULLIF(SUM(visits), 0) AS conversion_pct
FROM daily_traffic;
Keep both parts in your summary tables, not only the rate, so that any total can be rebuilt. NULLIF turns a zero
denominator into an empty result instead of an error.
The trap. AVG(100.0 * orders / visits) gives every day one vote: a Sunday with 50 visits weighs as much as Black
Friday with 4,000, and the rate can come out several times too high. A second trap hides in the WHERE: a filter
meant for the numerator also shrinks the denominator. Excluding cancelled orders from revenue also drops, from the
active customers, those whose only order was cancelled, and revenue per customer looks too good; give each part its
own condition, SUM(CASE WHEN status <> 'cancelled' THEN amount END) / COUNT(DISTINCT customer_id).
On your warehouse. With two integer sums, Postgres and Redshift divide as integers (7 / 2 is 3), while
DuckDB, BigQuery and Databricks return 3.5: the 100.0 * makes it a decimal everywhere. FILTER (WHERE …) is the
short form of the CASE on Postgres, Databricks and DuckDB.
The trap
WITH daily_traffic(day, visits, orders) AS ( VALUES (DATE '2025-11-28', 4000, 120), (DATE '2025-11-29', 3000, 90), (DATE '2025-11-30', 50, 10) ) SELECT ROUND(AVG(100.0 * orders / visits), 1) AS conversion_pct FROM daily_traffic;
| conversion_pct |
|---|
| 8.7 |
The fix
WITH daily_traffic(day, visits, orders) AS ( VALUES (DATE '2025-11-28', 4000, 120), (DATE '2025-11-29', 3000, 90), (DATE '2025-11-30', 50, 10) ) SELECT ROUND(100.0 * SUM(orders) / NULLIF(SUM(visits), 0), 1) AS conversion_pct FROM daily_traffic;
| conversion_pct |
|---|
| 3.1 |