D8LooPFocus modeCODE WORKSPACE
Return cohort_day, day_offset, and active_buyers: group buyers by their first order day (cohort), then count distinct active buyers at each whole-day offset from that cohort day. Order by cohort_day, day_offset.
first_order (date(MIN(created_at)) per buyer) joined to activity (DISTINCT buyer_id, date(created_at)); offset via julianday difference.
Sample input
| order_id | buyer_id | created_at |
|---|---|---|
| 1001 | 1 | 2026-01-12 08:40:00 |
| 1004 | 1 | 2026-01-15 14:20:00 |
| 1002 | 3 | 2026-01-12 10:02:00 |
| 1003 | 4 | 2026-01-13 11:15:00 |
| 1005 | 2 | 2026-01-18 09:05:00 |
Expected output
| cohort_day | day_offset | active_buyers |
|---|---|---|
| 2026-01-12 | 0 | 2 |
| 2026-01-12 | 3 | 1 |
| 2026-01-12 | 10 | 1 |
| 2026-01-12 | 16 | 1 |
| 2026-01-12 | 24 | 1 |
| 2026-01-13 | 0 | 1 |
Constraints
Build a first_order CTE (date(MIN(created_at)) per buyer) and an activity CTE (DISTINCT buyer_id, date(created_at)). Join on buyer_id, compute day_offset with CAST(julianday(activity_day) - julianday(cohort_day) AS INTEGER), and COUNT(DISTINCT buyer_id). Order by cohort_day, day_offset.
Expected skills
Cohort first-event logic, day offsets, and distinct counting.
Run the SQL query to inspect preview rows.