Chapter 8 · Window Functions
05. Days Between Consecutive Customer Orders
Medium20 XPE-Commerce
For each customer who has placed at least 2 orders, calculate the number of days between each consecutive order using LAG(). Return the customer_id, order_date, previous_order_date, and days_gap. Exclude the first order per customer (where there is no previous). Sort by customer_id, then order_date.
Tables
- PKidINT
- FKcustomer_id→ customers.idINT
- order_dateTEXT
- statusTEXT
- total_amountREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.