Pivot with conditional aggregation

One row per customer with a column for each status, built from SUM(CASE ...).

SQL · Pivot with conditional aggregationIntermediate
SELECT customer_id,
       SUM(CASE WHEN status = 'paid'      THEN total ELSE 0 END) AS paid,
       SUM(CASE WHEN status = 'pending'   THEN total ELSE 0 END) AS pending,
       SUM(CASE WHEN status = 'cancelled' THEN total ELSE 0 END) AS cancelled,
       COUNT(*) AS orders
FROM orders
GROUP BY customer_id;
pivotcasesumreport