SUM(CASE WHEN...) is a common form of conditional aggregation.
Example:
SELECT
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END)
FROM orders;This counts completed orders.
You can also calculate amounts:
SUM(
CASE
WHEN status = 'completed'
THEN amount
ELSE 0
END
)Same idea either way: aggregate only the rows that match a condition.
In short:
> Conditional aggregation means applying an aggregate function only to rows that satisfy a condition.