The situation
The growth team measures a three-step funnel: a user views a product, adds something to the cart, and buys. They want to know how many users get through each step within a week of first looking.
The existing query counts, for each step, the users who have at least one event of that type. That overstates every step after the first. Some users buy through a saved link without using the cart, some events are out of order, and some purchases come weeks after the view.
Input tables
events
One row per event.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | The user. |
event_type |
STRING | view, add_to_cart, purchase, and others such as search and wishlist. |
event_ts |
TIMESTAMP_NTZ | When the event happened. |
Requirement
Return one row for every user who has at least one view, showing how far through
the funnel they got.
first_view_tsis the user's earliestview.first_cart_tsis the user's earliestadd_to_cartthat is at or afterfirst_view_tsand no more than 7 days after it. NULL if there is none.first_purchase_tsis the user's earliestpurchasethat is at or afterfirst_cart_tsand no more than 7 days afterfirst_view_ts. NULL if there is none, and NULL wheneverfirst_cart_tsis NULL.- "No more than 7 days after" includes the moment exactly 7 days later.
- Events before the first view, and events of other types, are ignored.
Expected output
One row per user with at least one view.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | |
first_view_ts |
TIMESTAMP_NTZ | |
first_cart_ts |
TIMESTAMP_NTZ | NULL if the user did not reach this step. |
first_purchase_ts |
TIMESTAMP_NTZ | NULL if the user did not reach this step. |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
A funnel is a sequence. A user reached step 2 only if step 2 happened after step 1. So each step is defined in terms of the step before it, and the query is built the same way, one CTE per step:
first_view: the earliestviewper user. Everything else hangs off this.first_cart: join the user'sadd_to_cartevents to their first view, keep the ones at or after the view and within 7 days of it, and take the earliest.first_purchase: join the user'spurchaseevents to the cart found in step 2, keep the ones at or after that cart and within 7 days of the first view, and take the earliest.
Then start from first_view and LEFT JOIN the other two, so a user who stopped at
the first or second step still has a row, with NULLs.
Because first_purchase is built from first_cart, a user with no qualifying cart
cannot have a purchase. That rule comes for free from the structure.
The conditions are in the ON clause of each join, next to the event type. The
window is <= on both ends: an event at the same instant as the view counts, and so
does one exactly 7 days later.
There are 170 events from 58 users. 50 of them viewed something, 37 then added to the cart within the rules, and 21 of those purchased.
Common wrong answers
Each step on its own. MIN(CASE WHEN event_type = 'purchase' THEN event_ts END)
per user, and the same for the other two. This is the query from the story. Among
the 50 users with a view it reports 39 carts and 27 purchases instead of 37 and 21,
and the row is wrong for 7 users. It cannot see order or time: user 51 added to the
cart before ever viewing, user 52 bought without a cart, and user 57 did both
steps nine days later.
No 7-day limit. Wrong for 4 users: every late cart and late purchase is accepted.
< instead of <= on the limit. User 54 added to the cart exactly 7 days after
the view. The statement says that moment is included.
> instead of >= after the view. User 58's view and cart have the same
timestamp. With > the cart is not found, and then neither is the purchase.
The purchase only has to follow the view. User 55 has a purchase one hour after the view, then a cart at two hours, then another purchase at three. The funnel purchase is the one at three hours. Looking for the first purchase after the view returns the one that came before the cart.
The purchase found without requiring a cart. Joining purchases straight to the first view gives a purchase to users who never reached the cart step. Wrong for 3 users.
Seven days counted from the cart. The limit is measured from the first view for both steps. User 53 added to the cart on day 1 and bought on day 8: within a week of the cart, outside the week that started at the view. Wrong for 2 users.
Restarting the clock at a later view. User 56 came back after ten days and completed the funnel then. The statement anchors everything on the first view, so that second visit is outside the window. Whether a later visit should start a new funnel is a fair question for the product team; it is a different metric.
Inner joins. Only the 21 users who completed the funnel are returned. The other 29 rows are the ones that show where people drop out.
Reading the result
The funnel numbers are the non-NULL counts of the three columns: 50, 37, 21. Because each step is built on the previous one, they can only go down. If a funnel report ever shows a later step with more users than an earlier one, the steps were counted independently.
Edge cases in the data
| User | What they did | Expected |
|---|---|---|
| 51 | Cart two hours before the first view, purchase after it | No cart, no purchase. |
| 52 | View, then purchase, never a cart | No cart, no purchase. |
| 53 | Cart on day 1, purchase on day 8 | Cart found, purchase NULL. |
| 54 | Cart exactly 7 days after the view, purchase a minute later | Cart found, purchase NULL. |
| 55 | Purchase, then cart, then purchase | The second purchase. |
| 56 | Cart on day 1, then a new view, cart and purchase on day 10 | The day 1 cart, purchase NULL. |
| 57 | Cart and purchase on day 9 | No cart, no purchase. |
| 58 | View and cart at the same instant | Both found, and the purchase. |
| Users with no view at all | 8 users | Not in the result. |