Days Between Consecutive Orders
Medium
Window Functions
For each order return the number of days since the same customer's previous order (NULL for a customer's first order). Order by id.
Table schema
Orders(id INT PRIMARY KEY, customer_id INT, order_date DATE)Sample data
id | customer_id | order_date
1 | 1 | 2026-01-01
2 | 1 | 2026-01-11
3 | 1 | 2026-01-12
4 | 2 | 2026-02-01
Discussion
No discussions yet.
Shalyam Support
Hi! Ask us anything — we'll reply on WhatsApp or Telegram within 24 hours.