Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Return cohort_day, day_offset, and active_buyers based on first order day and later activity days.
Result columns · in this order
cohort_dayday_offsetactive_buyersHow to approach it
Find each buyer's first order day, then join to distinct activity days.
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 |
5 rows — all rows shown.
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 |
6 rows — all rows shown.
Constraints
Build first_order and activity CTEs, compute day offsets, and count distinct buyers.
Expected skills
Cohort logic, offsets, and distinct counting.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.