A product funnel is an ordered list of steps, and an event is logged each time a user hits a step. Build a funnel report with one row per step, ordered by step_order. For each step, show how many distinct users reached it, what share that is of the first step's users and of the previous step's users, and how many users were lost compared with the previous step.
Input Table(s)
Table funnel_steps (primary key step_id, all columns NOT NULL):
| Column | Type | Meaning |
|---|
| step_id | INT | Identifier of the step |
| step_order | INT | Position of the step in the funnel; unique positive integer |
| step_name | VARCHAR(64) | Display name of the step |
Table funnel_events (primary key event_id, all columns NOT NULL):
| Column | Type | Meaning |
|---|
| event_id | INT | Identifier of the event |
| user_id | INT | User who triggered the event |
| step_id | INT | Step the event belongs to, matching funnel_steps.step_id |
A user can have several events for the same step.
Output Contract
One row per row of funnel_steps, with these columns in this order:
| Column | Definition |
|---|
| step_order | The step's step_order |
| step_name | The step's step_name |
| users_reached | Number of distinct user_id values with at least one event at this step; 0 when there are none |
| overall_conversion_pct | 100 times users_reached divided by users_reached of the first step (smallest step_order) |
| step_conversion_pct | 100 times users_reached divided by users_reached of the previous step; 100.00 for the first step |
| dropped_users | Previous step's users_reached minus this step's users_reached; 0 for the first step |
Sort by step_order ascending. Round both percentage columns to 2 decimal places. When a percentage's denominator is 0, return 0.00.
Example
funnel_steps:
| step_id | step_order | step_name |
|---|
| 10 | 1 | View |
| 20 | 2 | Cart |
| 30 | 3 | Checkout |
funnel_events:
| event_id | user_id | step_id |
|---|
| 1 | 5 | 10 |
| 2 | 5 | 10 |
| 3 | 6 | 10 |
| 4 | 5 | 20 |
Expected output:
| step_order | step_name | users_reached | overall_conversion_pct | step_conversion_pct | dropped_users |
|---|
| 1 | View | 2 | 100.00 | 100.00 | 0 |
| 2 | Cart | 1 | 50.00 | 50.00 | 1 |
| 3 | Checkout | 0 | 0.00 | 0.00 | 1 |
View has 2 distinct users (user 5 counts once despite two events). Cart has 1 user, which is 50% of View and 50% of the previous step, so 1 user dropped. Checkout has no events, so it still appears with 0 users, 0.00 on both rates, and 1 dropped user.
Constraints and Clarifications
-
There are 1 to 20 steps.
step_order
values are unique positive integers and need not be consecutive, so "previous step" means the step with the next smaller
step_order
.
-
There are 0 to 10,000 events.
-
Repeated events by the same user at the same step count once.
-
A user counts at a step when they have any event there, whether or not they have events at earlier steps. A step can therefore reach more users than the step before it; report the values as computed, so a percentage can exceed 100 and
dropped_users
can be negative.
-
If the first step has 0 users, every
overall_conversion_pct
is
0.00
, and the first step's
step_conversion_pct
is still
100.00
.