Featurespace's ARIC Risk Hub analyzes transaction activity by user. Write a PostgreSQL query that calculates a 7-day rolling average of daily transaction counts for each active user during the reporting period from 2024-01-01 through 2024-01-10.
Requirements
- Generate one row for every active user and every calendar day in the reporting period, including days with no transactions.
- Count transactions per user per day, treating missing activity as zero.
- Use a window function partitioned by user and ordered by date to calculate the rolling average over the current day and previous six calendar days.
- Return only dates from 2024-01-04 onward, ordered by user and activity date.
Output details
- Return columns in this order:
user_id, activity_date, daily_transaction_count, rolling_7_day_average.
- Order results by
user_id ascending, then activity_date ascending.
- Round
rolling_7_day_average to four decimal places.
- Define active users as distinct non-NULL
user_id values with transactions from 2024-01-01 00:00:00 inclusive through 2024-01-11 00:00:00 exclusive.