18. Page load percentiles Solved
AdvancedHardquantile_contnested STRUCTGROUP BY ALL
For page_view events, return per browser and device type: browser, device ('mobile' or 'desktop', from device.mobile), views, median_ms and p90_ms: the continuous (interpolated) 50th and 90th percentiles of load_ms, rounded to whole milliseconds. Sort by browser, then device.
Your answer returns 8 rows with the columns browser, device, views, median_ms, p90_ms. 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
quantile_cont(load_ms, 0.9); median() is the continuous 50th percentile.Common traps
quantile_discreturns an existing value, not an interpolated one.
Solution
SELECT device.browser AS browser,
CASE WHEN device.mobile THEN 'mobile' ELSE 'desktop' END AS device,
count(*) AS views,
round(quantile_cont(load_ms, 0.5)) AS median_ms,
round(quantile_cont(load_ms, 0.9)) AS p90_ms
FROM web_events
WHERE event_type = 'page_view'
GROUP BY ALL
ORDER BY browser, device
Free for personal practice. Please don't republish the questions or solutions. See the Playground License Terms.