Conditional aggregation

Conditional aggregation computes several filtered aggregates in a single query by placing a CASE WHEN inside the aggregate function, or with PostgreSQL's FILTER clause. It produces, for example, one column per status in a single pass over the table.

Example

SELECT COUNT(*) FILTER (WHERE statut = 'payee') AS payees, SUM(CASE WHEN statut = 'annulee' THEN 1 ELSE 0 END) AS annulees FROM commandes;

Related concepts

The whole glossary