Task: the percentage of failed transactions for each type.
The percentage of failed transactions per type uses conditional aggregation: failed over total, with 100.0 as the first factor. Integer division in PostgreSQL drops the fraction, so COUNT(*) FILTER (WHERE …) / COUNT(*) * 100.0 gives 0. The denominator is agreed (whether pending and NULL count), protected from zero with NULLIF, and the result checked against a manual count.
SQL · Tasks
The failed-transaction percentage came out as 0, although failed ones certainly exist. What went wrong?
The error percentage per operation type is a typical QA metric: it shows where the system breaks most often. The query consists of conditional aggregation and a division, and the division hides the trap. The test table holds 9 transactions: 4 deposits (1 failed), 3 withdrawals (1 failed), 2 transfers (none failed).
How it works and why. COUNT(*) FILTER (WHERE status = 'failed') counts only the rows of the group that satisfy the condition, while COUNT(*) counts all. Both return an integer (bigint). Dividing an integer by an integer in PostgreSQL gives an integer and drops the fraction, so 1 / 4 is 0. The order of operations matters too: an expression is evaluated left to right, so failed / total * 100.0 first divides integers (0), then multiplies zero by 100.0, and the answer is 0 for every type. For a fractional division the first factor must be a number with a decimal part: 100.0 * failed / total.
Example: from a truncated 33 to 33.33.
SELECT type,
COUNT(*) AS total,
COUNT(*) FILTER (WHERE status = 'failed') AS failed,
COUNT(*) FILTER (WHERE status = 'failed') * 100 / COUNT(*) AS pct
FROM transactions
GROUP BY type
ORDER BY pct DESC;
-- withdrawal | 3 | 1 | 33
-- deposit | 4 | 1 | 25
-- transfer | 2 | 0 | 0
Diagnosis: the result looks plausible, and the error is masked by 25 and 0 being exactly right. Only the row with 33 for 1 out of 3 gives the truncation away. Checking against a manual calculation (1 of 3 is 33.33) shows the mismatch. The fix: the 100.0 factor first and rounding to the needed precision.
SELECT type,
COUNT(*) AS total,
COUNT(*) FILTER (WHERE status = 'failed') AS failed,
ROUND(100.0 * COUNT(*) FILTER (WHERE status = 'failed')
/ COUNT(*), 2) AS failed_pct
FROM transactions
GROUP BY type
ORDER BY failed_pct DESC;
-- withdrawal | 3 | 1 | 33.33
-- deposit | 4 | 1 | 25.00
-- transfer | 2 | 0 | 0.00
-- the same without FILTER (MySQL, SQL Server)
SELECT type,
ROUND(AVG(CASE WHEN status = 'failed'
THEN 100.0 ELSE 0 END), 2) AS failed_pct
FROM transactions
GROUP BY type;
The same percentage, two ways to write it
| type | total | failed | pct |
|---|---|---|---|
| withdrawal | 3 | 1 | 33 |
| deposit | 4 | 1 | 25 |
| transfer | 2 | 0 | 0 |
| type | total | failed | pct |
|---|---|---|---|
| withdrawal | 3 | 1 | 33.33 |
| deposit | 4 | 1 | 25.00 |
| transfer | 2 | 0 | 0.00 |
Limits and alternatives. The AVG(CASE …) variant works where there is no FILTER: the average of 100 for failed and 0 for the rest equals the percentage of failed. The denominator here is all rows of the group, including NULL statuses. This is a decision about the metric, not about syntax: sometimes the percentage is counted only from final statuses (completed and failed), without pending, and then the denominator condition must be stated explicitly. If a group can be empty (for example a dictionary of types with transactions attached by LEFT JOIN), the denominator is zero and PostgreSQL raises "division by zero": wrap the denominator in NULLIF(COUNT(t.id), 0).
Common mistakes.
- Dividing integers: the fraction disappears, and on small groups the percentage becomes 0.
failed / total * 100.0: the order of evaluation again puts the integer division first.- Counting the denominator only over filtered rows:
WHERE status = 'failed'leaves only the failed, and the percentage is always 100. - Not agreeing whether pending and NULL belong in the denominator.
Check yourself
What does COUNT(*) FILTER (WHERE status = 'failed') / COUNT(*) * 100.0 return for the deposit type (1 of 4) and why?
Answer
0.0. First the integer division 1 / 4 runs and gives 0, then 0 * 100.0 gives 0.0. The multiplication by 100.0 comes too late. Put 100.0 as the first factor or cast to numeric.
In a types dictionary with a LEFT JOIN to transactions, the type "refund" has no transactions. What happens to the percentage and how to fix it?
Answer
The denominator COUNT(t.id) is 0 and division by zero raises an error. The fix: NULLIF(COUNT(t.id), 0) in the denominator, then the percentage for the empty type is NULL instead of an error.