Chapter 9 · Interview Level
22. 3-Order Moving Average Per Customer
Medium20 XPE-Commerce
Calculate a 3-order moving average of total_amount for each customer. The moving average should include the current order and the two preceding orders (by date). Return customer_id, order_date, total_amount, and moving_avg_3 (rounded to 2 decimal places). Orders on the same date count in order id order. 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.