Deviopath › PostgreSQL

Totals by Status, Side by Side

Medium · Aggregation

A pivot turns values from rows into columns. A classic way is one SUM(CASE WHEN ... THEN value ELSE 0.00 END) per column: each CASE only lets through the rows for its column.

For every customer who has at least one order, return customer (the name), paid, pending and cancelled: the sum of total for orders with that status. A status with no orders must show 0.00 (use ELSE 0.00, not ELSE 0, so the result keeps two decimals). Sort by customer.

Next: Clear Out Cancelled Items