28. Abandoned carts in one pass Solved
PerformanceHardconditional aggregationarg_minsingle scan
A session abandoned its cart if it has an add_to_cart but no checkout. A session's browser is the browser of its first event (by ts, then event_id). Return browser and abandoned (number of abandoned sessions), sorted by abandoned descending, then browser. Constraint: read web_events once.
Your answer returns 2 rows with the columns browser, abandoned. 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
bool_or(...) flags plus arg_min(device.browser, (ts, event_id)).Common traps
NOT EXISTS (SELECT … checkout …)or a self-join reads the events twice.
Solution
WITH s AS (
SELECT session_id,
arg_min(device.browser, (ts, event_id)) AS browser,
bool_or(event_type = 'add_to_cart') AS carted,
bool_or(event_type = 'checkout') AS checked_out
FROM web_events
GROUP BY session_id
)
SELECT browser, count(*) AS abandoned
FROM s
WHERE carted AND NOT checked_out
GROUP BY browser
ORDER BY abandoned DESC, browser
Why it is fast
The slower version returns exactly the same rows:
WITH first_event AS (
SELECT session_id, device.browser AS browser
FROM web_events
QUALIFY row_number() OVER (PARTITION BY session_id ORDER BY ts, event_id) = 1
)
SELECT f.browser, count(*) AS abandoned
FROM first_event f
WHERE EXISTS (SELECT 1 FROM web_events e WHERE e.session_id = f.session_id AND e.event_type = 'add_to_cart')
AND NOT EXISTS (SELECT 1 FROM web_events e WHERE e.session_id = f.session_id AND e.event_type = 'checkout')
GROUP BY f.browser
ORDER BY abandoned DESC, f.browser
How this is graded: on DuckDB's physical plan of your final statement (EXPLAIN (FORMAT JSON)): web_events is scanned at most once.
Free for personal practice. Please don't republish the questions or solutions. See the Playground License Terms.