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 buyer_id, order_id, created_at, previous_order_at, and days_since_previous.
Result columns · in this order
buyer_idorder_idcreated_atprevious_order_atdays_since_previousHow to approach it
Use LAG(created_at) over buyer order history.
Sample input
| order_id | buyer_id | created_at |
|---|---|---|
| 1001 | 1 | 2026-01-12 08:40:00 |
| 1004 | 1 | 2026-01-15 14:20:00 |
| 1009 | 1 | 2026-01-28 18:00:00 |
| 1005 | 2 | 2026-01-18 09:05:00 |
| 1011 | 2 | 2026-02-02 09:00:00 |
5 rows — all rows shown.
Expected output
| buyer_id | order_id | created_at | previous_order_at | days_since_previous |
|---|---|---|---|---|
| 1 | 1001 | 2026-01-12 08:40:00 | null | null |
| 1 | 1004 | 2026-01-15 14:20:00 | 2026-01-12 08:40:00 | 3 |
| 1 | 1009 | 2026-01-28 18:00:00 | 2026-01-15 14:20:00 | 13 |
| 2 | 1005 | 2026-01-18 09:05:00 | null | null |
| 2 | 1011 | 2026-02-02 09:00:00 | 2026-01-18 09:05:00 | 14 |
5 rows — all rows shown.
Constraints
Use LAG within each buyer ordered by created_at, then julianday date math.
Expected skills
Offset windows and timestamp comparisons.
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.