Conditional Aggregation Multiple Metrics
Compute multiple metrics in a single pass using conditional aggregation with CASE inside SUM for pivoted reporting.
Code
SELECT
region,
SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) AS active_count,
SUM(CASE WHEN status = 'inactive' THEN 1 ELSE 0 END) AS inactive_count,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending_count,
COUNT(*) AS total_count
FROM accounts
GROUP BY region
ORDER BY region;Report a problem with this exampleOpens a pre-filled issue on GitHub.