Maxim Mironjuk
-
September 23, 2025
Conditional aggregation is one of the most common needs in reporting queries: how many orders are cancelled, how many completed, how many still open, all in a single row per group. The classic path there runs through COUNT combined with a CASE WHEN expression that returns either a value or null depending on the condition. The SQL standard has offered a noticeably clearer alternative for a while now: the FILTER clause, attached directly to the aggregate function. This article covers how FILTER works, why it beats CASE WHEN for readability in many cases, how it combines with window functions for several parallel metrics, and where database availability draws the line.