Generally, the filter
clause can be implemented using a case
expression inside the aggregate function: the filter condition has to be put into the when
-clause, the value to be aggregated into the then
clause. Because aggregate functions generally skip over null
values,1 the implicit else null
clause is enough to ignore non-matching rows. The following two expressions are equivalent:
SUM(<expression>) FILTER(WHERE <condition>)
SUM(CASE WHEN <condition> THEN <expression> END)
Count(*)
needs some special treatment because “*” cannot be put into the then
clause. Instead, it is enough to use a non-null
constant value. This ensures that every matching row is counted. The implicit else null
clause maps non-matching rows to null
, which is ignored by count
too.
COUNT(*) FILTER (WHERE <condition>)
COUNT(CASE WHEN <condition> THEN 1 END)
When using a set quantifier (distinct
or all
) it must remain in the aggregate function prior the case
expression.
… to subscribe my mailing lists, get free stickers, buy my book or join a training.
The PostgreSQL database supports subqueries inside the filter
clause (e.g., via exists
).
As the above described alternative with the case
expression is very widely supported I recommend using that approach rather than the proprietary alternatives offered by some products.
Countif
(Google BigQuery)Google BigQuery offers the aggregate function countif
similar to Microsoft Excel.
COUNTIF(<condition>) [OVER(…)]
The same result can be obtained with the case
approach, which is very widely supported.
COUNT(CASE WHEN <condition> THEN 1 END) [OVER(…)]
You can’t catch up on 20 years of SQL evolution in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up and to keep modern-sql.com on your radar.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »
ISO/IEC 9075-2:2023 §10.9 SR 8.
When emulating using case
only window functions are disallowed. ISO/IEC 9075-2:2023 §10.9 SR 7a.
ISO/IEC 9075-2:2023 §10.9 GR 7a, 8a, 10d. See also ISO/IEC 9075-2:2023 §4.18.4
Exception: array_agg
(ISO/IEC 9075-2:2023 §10.9 GR 12gii (NOTE 528)) — the subquery array construction can remove null
values without filter
clause.