Problems / Status Count Pivot / Editorial
region
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END)
SUM
ELSE 0
0
COUNT(CASE WHEN status = 'cancelled' THEN 1 END)
SUM(CASE WHEN ... THEN 1 ELSE 0 END) is the manual pivot — the most portable way to turn a categorical column's values into separate columns. It needs one expression per known category, all computed in a single scan, with no join or subquery per status. Compare it to filtering: WHERE status = 'cancelled' GROUP BY region would silently *drop* regions that have no cancelled orders, while the conditional aggregate keeps every region and shows an honest 0. Some engines offer PIVOT or FILTER (WHERE ...) syntax as sugar, but the CASE trick works everywhere — SQL, pandas, and Spark alike.
SUM(CASE WHEN ... THEN 1 ELSE 0 END)
WHERE status = 'cancelled' GROUP BY region
PIVOT
FILTER (WHERE ...)
Solve Status Count Pivot yourself →