Chapter 8 · Window Functions
16. Compare Order Value to Previous Order
Hard40 XPE-Commerce
For each customer's orders, show how the order value compares to their previous order. Return customer_id, order_id, order_date, total_amount, the previous order's amount, and the difference. Use LAG to access the previous row (orders on the same date are ordered by order id). 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.