25. Pivot in one pass Solved
PerformanceMediumFILTER clausePIVOTsingle scan
Return one row per month of 2024: month ("YYYY-MM") and the number of orders in each status as columns placed, paid, shipped, delivered, returned (0 when none). Sort by month. Constraint: read orders once. No UNION ALL of per-status queries, no subquery per column.
Your answer returns 12 rows with the columns month, placed, paid, shipped, delivered, returned. Order matters.
The data
The store from the DuckDB playground, running in your browser with DuckDB. Prices are in EUR; the data covers 2024.
customers20 rows
id | INTEGER primary key |
name | VARCHAR |
country | VARCHAR |
currency | VARCHAR EUR, GBP, USD or JPY |
signed_up | DATE |
tags | VARCHAR[] a list: vip, newsletter, b2b, returns-often |
products10 rows
id | INTEGER primary key |
name | VARCHAR |
category | VARCHAR |
price | DECIMAL(10,2) EUR |
specs | STRUCT weight_g INTEGER, color VARCHAR, sizes VARCHAR[] |
orders300 rows
id | INTEGER primary key |
customer_id | INTEGER → customers.id |
placed_at | TIMESTAMP 2024 |
status | ENUM placed, paid, shipped, delivered, returned |
currency | VARCHAR the customer's currency |
order_items576 rows
order_id | INTEGER → orders.id |
product_id | INTEGER → products.id |
quantity | INTEGER |
unit_price | DECIMAL(10,2) EUR |
web_events20,000 rows
event_id | BIGINT |
ts | TIMESTAMP |
session_id | VARCHAR |
customer_id | INTEGER NULL for anonymous visitors |
event_type | VARCHAR page_view, search, add_to_cart, checkout |
page | STRUCT path, referrer |
device | STRUCT browser, mobile BOOLEAN, screen STRUCT(w, h) |
product_ids | INTEGER[] |
load_ms | INTEGER |
fx_rates720 rows
currency | VARCHAR USD, GBP, JPY |
valid_from | TIMESTAMP |
per_eur | DOUBLE units of currency per 1 EUR |
Hints
count(*) FILTER (WHERE status = 'paid') AS paid per column, or DuckDB's PIVOT.Common traps
- One query per status glued with UNION ALL / joins scans the table five times.
Solution
SELECT strftime(date_trunc('month', placed_at), '%Y-%m') AS month,
count(*) FILTER (WHERE status = 'placed') AS placed,
count(*) FILTER (WHERE status = 'paid') AS paid,
count(*) FILTER (WHERE status = 'shipped') AS shipped,
count(*) FILTER (WHERE status = 'delivered') AS delivered,
count(*) FILTER (WHERE status = 'returned') AS returned
FROM orders
WHERE placed_at >= TIMESTAMP '2024-01-01' AND placed_at < TIMESTAMP '2025-01-01'
GROUP BY month
ORDER BY month
Why it is fast
The slower version returns exactly the same rows:
WITH m AS (SELECT DISTINCT strftime(date_trunc('month', placed_at), '%Y-%m') AS month FROM orders WHERE year(placed_at) = 2024)
SELECT month,
(SELECT count(*) FROM orders WHERE strftime(date_trunc('month', placed_at), '%Y-%m') = m.month AND status = 'placed') AS placed,
(SELECT count(*) FROM orders WHERE strftime(date_trunc('month', placed_at), '%Y-%m') = m.month AND status = 'paid') AS paid,
(SELECT count(*) FROM orders WHERE strftime(date_trunc('month', placed_at), '%Y-%m') = m.month AND status = 'shipped') AS shipped,
(SELECT count(*) FROM orders WHERE strftime(date_trunc('month', placed_at), '%Y-%m') = m.month AND status = 'delivered') AS delivered,
(SELECT count(*) FROM orders WHERE strftime(date_trunc('month', placed_at), '%Y-%m') = m.month AND status = 'returned') AS returned
FROM m ORDER BY month
How this is graded: on DuckDB's physical plan of your final statement (EXPLAIN (FORMAT JSON)): orders is scanned at most once.
Free for personal practice. Please don't republish the questions or solutions. See the Playground License Terms.