Chapter 8 · Window Functions
44. Per-Customer 3-Transaction Moving Average
Hard40 XPFinance
For each customer, compute a 3-transaction moving average of withdrawal amounts. Return first_name, last_name, transaction_date, amount, and moving_avg_3 (rounded to 2 decimals). The moving average should consider the current and two preceding withdrawal transactions ordered by transaction id. Only include withdrawal transactions. Order by last_name, transaction_date.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- emailTEXT
- phoneTEXT
- date_of_birthTEXT
- cityTEXT
- stateTEXT
- countryTEXT
- created_atTIMESTAMP
- PKidINT
- FKcustomer_id→ customers.idINT
- account_typeTEXT
- balanceREAL
- currencyTEXT
- opened_atTEXT
- statusTEXT
- PKidINT
- FKaccount_id→ accounts.idINT
- transaction_dateTEXT
- typeTEXT
- amountREAL
- balance_afterREAL
- descriptionTEXT
- reference_numberTEXT
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.