Streaming Funnel by Impression Day
An events(event_ts, user_id, event_type, show_id, session_id, revenue_cents, campaign_id) table holds a streaming product's funnel events, with event_type in impression, click, signup, start_subscription. Build a daily funnel for every day d from 2025-08-26 to 2025-09-01 inclusive, attributed to the impression day:
- a user counts as an impression on
dif they have at least one impression that day; - as a click if they also have a click on
dord + 1; - as a signup if they have a signup within 3 days after that click;
- as a subscription if they have a
start_subscriptionwithin 7 days after that signup.
Count each user at most once per impression day. Output day, impressions, clicks_w1d, signups_w3d, subs_w7d, click_rate, signup_rate, sub_rate, where each rate divides a stage by the one before it. Write ANSI-style SQL that runs on Snowflake or BigQuery, and say what partitioning or clustering of events would keep it fast at billions of rows.
Hints
Hint 1
Reduce each stage to distinct (user_id, date) pairs first; the funnel is then a chain of left joins with a date-range condition at each hop.
Hint 2
Chain the windows from the previous stage's event, not from the impression: the signup window starts at the click, the subscription window at the signup.
Hint 3
Deduplicate after the joins (a user with two qualifying clicks must still count once) with COUNT(DISTINCT user_id) per stage, or by collapsing to the earliest qualifying event at each hop before joining the next.
Solution
Show the approach
Approach
Turn each event type into a set of distinct (user_id, event_date) pairs, then chain left joins with the window conditions, and count distinct users per stage per impression day. Taking the earliest qualifying click per impression day keeps the join from fanning out; the same for signups.
WITH imp AS (
SELECT DISTINCT user_id, CAST(event_ts AS DATE) AS d
FROM events WHERE event_type = 'impression'
AND CAST(event_ts AS DATE) BETWEEN DATE '2025-08-26' AND DATE '2025-09-01'
), clk AS (SELECT DISTINCT user_id, CAST(event_ts AS DATE) AS d FROM events WHERE event_type = 'click'),
sgn AS (SELECT DISTINCT user_id, CAST(event_ts AS DATE) AS d FROM events WHERE event_type = 'signup'),
sub AS (SELECT DISTINCT user_id, CAST(event_ts AS DATE) AS d FROM events WHERE event_type = 'start_subscription'),
first_click AS (
SELECT i.user_id, i.d, MIN(c.d) AS click_d
FROM imp i LEFT JOIN clk c
ON c.user_id = i.user_id AND c.d BETWEEN i.d AND i.d + INTERVAL '1' DAY
GROUP BY i.user_id, i.d
), first_signup AS (
SELECT f.user_id, f.d, f.click_d, MIN(s.d) AS signup_d
FROM first_click f LEFT JOIN sgn s
ON s.user_id = f.user_id AND s.d BETWEEN f.click_d AND f.click_d + INTERVAL '3' DAY
GROUP BY f.user_id, f.d, f.click_d
), funnel AS (
SELECT g.user_id, g.d, g.click_d, g.signup_d, MIN(b.d) AS sub_d
FROM first_signup g LEFT JOIN sub b
ON b.user_id = g.user_id AND b.d BETWEEN g.signup_d AND g.signup_d + INTERVAL '7' DAY
GROUP BY g.user_id, g.d, g.click_d, g.signup_d
)
SELECT d AS day,
COUNT(*) AS impressions,
COUNT(click_d) AS clicks_w1d,
COUNT(signup_d) AS signups_w3d,
COUNT(sub_d) AS subs_w7d,
COUNT(click_d) * 1.0 / NULLIF(COUNT(*), 0) AS click_rate,
COUNT(signup_d) * 1.0 / NULLIF(COUNT(click_d), 0) AS signup_rate,
COUNT(sub_d) * 1.0 / NULLIF(COUNT(signup_d), 0) AS sub_rate
FROM funnel
GROUP BY d
ORDER BY d;Each CTE after imp keeps one row per (user_id, impression day), so the final COUNTs are already per user per day and need no DISTINCT. A NULL click leaves the later windows NULL, which is the funnel falling through.
Worth saying out loud
- Earliest-event collapsing is a choice: taking the latest qualifying click gives a different signup window. State it.
- Date arithmetic differs by engine:
DATEADD(day, 1, d)on Snowflake,DATE_ADD(d, INTERVAL 1 DAY)on BigQuery; theINTERVALform above is the portable intent. - At billions of rows, partition
eventsby event date and cluster byevent_type, user_id; each CTE then prunes to a handful of days, and the joins run per user within them. Pre-aggregating the daily distinct(user_id, type, date)table once a day turns the whole query into joins on a table a thousand times smaller.
Assistant
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.