Chapter 8 · Window Functions
21. Customer Order Sequence Number
Hard40 XPE-Commerce
For each order, show the order id, customer_id, order_date, total_amount, and the sequence number of that order for the customer (1st order, 2nd order, etc.). Also show the previous order's total_amount using LAG. If a customer placed two orders on the same date, the lower order id comes first. Sort by customer_id, then sequence number.
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.