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.
Schema
🔑idINT
🔗customer_idINT
order_dateTEXT
statusTEXT
total_amountREAL
postgresql
Run your query to see results
Press Ctrl + Enter to execute