
A completion report shows zero even though three of eight tasks are done. Before changing the chart, run the arithmetic by itself. In SQLite, division between two integers produces an integer result, truncated toward zero. A decimal-looking display added afterward cannot bring the lost fraction back.
Move the conversion before the division
This read-only query isolates the difference without touching a table:
SELECT
3 / 8 AS integer_ratio,
CAST(3 / 8 AS REAL) AS too_late,
CAST(3 AS REAL) / 8 AS real_ratio;
-- 0 | 0.0 | 0.375
The second expression converts a result that is already zero. The third converts an operand first, so the division uses floating-point arithmetic. I would use the explicit CAST form in a report query because it makes that choice visible. Multiplying by 1.0 before dividing also works here; multiplying the completed integer quotient by 1.0 does not.
Aggregates can keep the integer behavior
Adding integer counts with SUM does not automatically produce a fractional value. For ordinary values that do not overflow, SUM of integer inputs remains an integer. This fixture represents two groups with different sizes:
WITH counts(done, total) AS (
VALUES (1, 2), (2, 6)
)
SELECT
SUM(done) / SUM(total) AS truncated,
CAST(SUM(done) AS REAL)
/ NULLIF(SUM(total), 0) AS overall_ratio
FROM counts;
-- 0 | 0.375
That overall ratio is three completed tasks out of eight. It is not the same calculation as averaging each group's completion rate. The unweighted average of 1/2 and 2/6 is about 0.4167 because it gives the small group and large group equal weight. Choose the business meaning before fixing the numeric type.
Keep an undefined ratio distinct from zero
NULLIF returns NULL when its two arguments are equal. In this expression, a zero total becomes NULL, and the division result stays NULL. SQLite already returns NULL for division by zero, so NULLIF is not rescuing an exception here. It documents the denominator rule.
A zero completion rate means there were opportunities but none completed. No opportunities is a different state. Do not wrap the ratio in COALESCE(..., 0) unless the report deliberately treats those states as equivalent. A chart or export also needs a defined way to display the missing ratio.
Inspect values instead of trusting column names
SELECT done, typeof(done), total, typeof(total)
FROM your_counts
LIMIT 10;
Replace your_counts with the actual table or view. This samples storage classes, not the entire dataset. Audit all relevant rows before relying on a type assumption. Casting is not input validation: for example, CAST('unknown' AS REAL) returns 0.0 rather than rejecting the text. Fix malformed source data or reject it through an explicit validation rule.
What I checked and what this does not promise
I ran the synthetic expressions in SQLite 3.37.2. They returned the values shown; the grouped comparison returned an integer SUM, and both direct zero-denominator division and the NULLIF version returned NULL. No production database was opened or changed.
REAL arithmetic is approximate, not an exact-decimal money strategy. This change also does not prevent integer overflow inside SUM before the cast happens. For a routine count ratio, check operand types, conversion placement, denominator meaning and the expected result on a small fixture before changing the report.
Comments
Post a Comment