Measure a signup funnel in SQLite
Count the people who move through three ordered events in a defined observation window.
Create the sample events
The funnel looks for a view, a signup start and a signup completion for the same person in that order. Include people whose first view falls in 100 ≤ time < 200. All later funnel events must occur before 200. The event ID orders events with the same timestamp.
sqlite3 :memory:CREATE TABLE events (
event_id INTEGER PRIMARY KEY,
person_id TEXT NOT NULL,
event_time INTEGER NOT NULL,
kind TEXT NOT NULL
);
INSERT INTO events VALUES
(1, 'A', 110, 'view'),
(2, 'A', 111, 'view'),
(3, 'A', 120, 'started'),
(4, 'A', 130, 'completed'),
(5, 'B', 115, 'view'),
(6, 'B', 116, 'completed'),
(7, 'B', 117, 'started'),
(8, 'C', 140, 'view'),
(9, 'C', 140, 'started'),
(10, 'C', 150, 'completed'),
(11, 'D', 160, 'view'),
(12, 'D', 200, 'started'),
(13, 'E', 145, 'started'),
(14, 'E', 146, 'completed'),
(15, 'F', 90, 'view'),
(16, 'F', 121, 'view');
Count each person once per step
Find the first view over all observed history. Then take the first qualifying start after it and the first qualifying completion after that. Reading earlier views lets you exclude people who first viewed before the window.
WITH ordered AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY person_id ORDER BY event_time, event_id
) AS position
FROM events
WHERE event_time < 200
),
first_view AS (
SELECT person_id, MIN(position) AS view_position
FROM ordered
WHERE kind = 'view'
GROUP BY person_id
),
cohort AS (
SELECT g.person_id, g.view_position
FROM first_view AS g
JOIN ordered AS s
ON s.person_id = g.person_id
AND s.position = g.view_position
WHERE s.event_time >= 100
),
starts AS (
SELECT g.person_id, MIN(s.position) AS start_position
FROM cohort AS g
JOIN ordered AS s ON s.person_id = g.person_id
WHERE s.kind = 'started'
AND s.position > g.view_position
GROUP BY g.person_id
),
completions AS (
SELECT b.person_id, MIN(s.position) AS completion_position
FROM starts AS b
JOIN ordered AS s ON s.person_id = b.person_id
WHERE s.kind = 'completed'
AND s.position > b.start_position
GROUP BY b.person_id
)
SELECT
COUNT(*) AS view,
COUNT(b.person_id) AS people_started,
COUNT(t.person_id) AS people_completed,
ROUND(
100.0 * COUNT(t.person_id) / NULLIF(COUNT(*), 0), 1
) AS conversion_pct
FROM cohort AS g
LEFT JOIN starts AS b ON b.person_id = g.person_id
LEFT JOIN completions AS t ON t.person_id = g.person_id;
Verify the counts by hand
A and C complete all three steps. B starts, but the completion precedes its start. D only contributes a view because the start lands on the upper boundary. E has no view, and F’s first view is too early. Expect four viewers, three starts and two completions for a 50% completion rate.