Deviopath › PostgreSQL

Paid vs Pending

Medium · Aggregation

An aggregate can take a FILTER clause: COUNT(*) FILTER (WHERE status = 'paid') counts only the rows that match, while other aggregates in the same SELECT still see the whole group.

For every customer who has at least one order, return name, paid_orders (orders with status paid) and pending_orders (orders with status pending). Cancelled orders are not counted in either column. Sort by name.

Next: Big Spenders with a CTE