How Can One SQL Report Count Different Conditions Without Losing the Overall Total?
An operations dashboard needs all requests, urgent requests and overdue requests. A developer filters the query to urgent records to build the urgent count. The total then shrinks, and the dashboard compares measures that no longer describe the same population.
Separate the report's eligible population from the condition used by each measure. That distinction makes conditional aggregation easier to review.
Define the shared population
Start with a sentence such as “requests received by this team during the selected reporting period.” Decide how canceled, test and duplicate records are handled before calculating any measure.
PostgreSQL's aggregate-expression documentation describes FILTER as limiting the rows fed into an individual aggregate when its condition is true.
This allows several aggregates to use different conditions while remaining in one grouped query. It does not correct a poorly chosen base population or an accidental multiplication of rows upstream.
Make a fixture that exposes overlap
Illustrative example: five requests
Create five fictional requests. Two are urgent, three are overdue, and one belongs to both groups. The expected total is five; the urgent count is two; the overdue count is three.
Adding urgent and overdue gives five in this fixture, but that coincidence does not make them an exclusive partition. Their union contains four distinct requests because one is counted twice across the two measures.
Add a sixth ordinary, on-time request. The two conditional counts stay unchanged while the overall total becomes six. This catches a query that incorrectly restricts the whole population.
Test a missing status
Include a record whose priority or due-date assessment is unknown. Decide whether it belongs in the total and whether a separate “not assessed” measure is needed.
Do not silently interpret unknown priority as ordinary priority. The count should preserve the agreed meaning of each state.
Keep filters at the correct level
A report-wide condition belongs to the shared population definition. A condition specific to one measure belongs to that measure. A rule deciding which completed groups to display is another level again.
Write those three purposes in the review notes. Reviewers should not have to infer them from similarly named WHERE clauses.
For the distinction between row and group filtering, read WHERE versus HAVING in reporting.
Check the grain before the arithmetic
If one request has several related events, a join can produce several rows for that request. Conditional counts over those rows may count events rather than requests.
Choose the reporting grain explicitly. Prepare one row per request, or use an appropriately justified distinct-identity calculation. Do not add DISTINCT merely to hide an unexplained increase.
Include one request with multiple events in the fixture. Its expected contribution should remain clear for every metric.
Label what the reader can add
If measures overlap, say so beside the chart or table. Do not display them as slices of a whole unless the categories are mutually exclusive and collectively describe that whole.
For acceptance, compare the SQL output with a hand-marked fixture showing each request's membership in every measure. Check the total, each conditional count and the union where relevant.
Keep the fixture with the report definition. It becomes a compact regression check when someone adds a new status or another conditional measure.
Editorial note
Prepared with AI assistance. Primary sources and the FutureOS article inventory were checked on October 26, 2026. Examples are illustrative, not customer results. FutureOS is described as being in prelaunch in its current introduction. This guide does not claim an available product feature, integration or service, and no independent expert review is claimed.




Comments