▸_sqlgym

75. Pivot Order Status Counts

MediumPivotingconditional aggregationCOUNTSUM

Pivoting with a count uses SUM(CASE WHEN status='X' THEN 1 ELSE 0 END) — each matching row contributes 1, each other row contributes 0, so the sum is the number of rows with that status. This turns the status column into one count column per status value.

Task: the orders table has one row per order. For each customer, count their orders in each status. Return customer, shipped, pending, cancelled (counts; use ELSE 0 so a customer with none of a status shows 0, not NULL). Order by customer.

Output columns, in order: customer, shipped, pending, cancelled.

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.