Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Finance want a small grid: one row per order status, one column per month, revenue in the cells. They will paste it into a slide, so the shape matters as much as the numbers.
Return status, jan, feb and mar for the three months in the table, ordered by status.
Result columns · in this order
status | Normalised order status: lower case, no padding. |
jan | Revenue for orders placed in January 2026. |
feb | Revenue for orders placed in February 2026. |
mar | Revenue for orders placed in March 2026. |
How to approach it
One SUM(CASE WHEN month-range THEN order_total ELSE 0 END) per column, grouped on a cleaned status.
Sample input
| order_id | customer_id | placed_at | status | order_total |
|---|---|---|---|---|
| 1001 | c1 | 2026-01-05 10:00:00 | paid | 120 |
| 1002 | c2 | 2026-01-12 14:30:00 | Paid | 80 |
| 1003 | c1 | 2026-01-20 09:15:00 | pending | 45 |
| 1004 | c3 | 2026-01-31 23:30:00 | PAID | 200 |
| 1005 | c2 | 2026-02-01 00:15:00 | paid | 60 |
| 1006 | c4 | 2026-02-03 11:00:00 | paid | 150 |
| 1007 | c3 | 2026-02-10 16:45:00 | refunded | 90 |
| 1008 | c1 | 2026-02-14 12:00:00 | paid | 300 |
| 1009 | c5 | 2026-02-18 08:30:00 | Paid | 75 |
| 1010 | c4 | 2026-02-25 19:20:00 | pending | 85 |
| 1011 | c5 | 2026-02-28 21:00:00 | paid | 130 |
| 1012 | c2 | 2026-03-02 10:10:00 | paid | 95 |
12 rows — scroll inside the table to see them all.
Expected output
| status | jan | feb | mar |
|---|---|---|---|
| paid | 400 | 715 | 95 |
| pending | 45 | 85 | 0 |
| refunded | 0 | 90 | 0 |
3 rows — all rows shown.
Constraints
PIVOT clause. Build the columns with conditional aggregation — one SUM(CASE WHEN ...) per month.paid, Paid, PAID and paid all appear. Group on a normalised value or the grid comes back with five rows instead of three.0, not NULL — a blank cell in a slide reads as missing data rather than as no sales.>= start AND < next_start). placed_at carries a time, so BETWEEN on dates drops the last day.status.Worked example
For the paid row, January is the sum of order_total over every paid order placed before 2026-02-01 — orders 1001 (120) and 1004 (200), plus 1002 (80) which is stored as Paid. That is 400.00.
Miss the normalisation and you get three separate rows — paid 260, Paid 80, PAID 200 — each of which looks like a legitimate status to a reader who was not there.
The ELSE 0 is what turns an empty cell into a zero: without it the CASE returns NULL, SUM skips NULLs, and a status with no March revenue reports NULL rather than 0.00.
What this tests
Whether you can pivot without a PIVOT clause, and whether you noticed that the grouping key needs cleaning before it becomes row labels.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.