Scotiabank's customer analytics team wants to monitor recent transaction behaviour. Write a PostgreSQL query that calculates a rolling 30-day average transaction amount for each active customer.
Use the current transaction date plus the preceding 29 calendar days, and calculate the average across non-null transactions in that window.
transactions to customers and include only active customers.customer_id, customer_name, transaction_date, amount, and rolling_30_day_avg, in that order.customer_id, transaction_date, and tied transaction_id values in ascending order.| Column | Type | Description |
|---|---|---|
| customer_idPK | INTEGER | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer's name |
| is_active | BOOLEAN | Whether the customer is currently active |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INTEGER | Unique transaction identifier |
| customer_id | INTEGER | Customer associated with the transaction |
| transaction_date | DATE | Date the transaction occurred |
| amount | NUMERIC(12,2) | Transaction amount |