Problem Statement
What is a common pattern to compute conditional totals in a single pass?
Explanation
Conditional aggregation uses CASE to route rows into different buckets, then aggregates once. It is efficient and expressive.
On PostgreSQL, FILTER(...) is an alternative, but CASE works across engines.
Code Solution
SolutionRead Only
SELECT SUM(CASE WHEN status='PAID' THEN amount ELSE 0 END) AS paid_amt, SUM(CASE WHEN status='REFUND' THEN amount ELSE 0 END) AS refund_amt FROM invoices;
Practice Sets
This question appears in the following practice sets:
