Business Intelligence Path · Mission 22 of 25Expert

Three-month moving average

Use window frame specifications to compute moving averages

Back to Business Intelligence

The Brief

Rachel TorresCFOfinance-analytics

Monthly revenue is too noisy for the board — they want a smoothed trend line. Compute a 3-month trailing moving average alongside the raw monthly revenue. For each month, show year, month, monthly revenue, and the average of that month plus the two preceding months. The first two months will have fewer than 3 data points — that's fine, just average what's available.

You'll practice

Window frameMoving average

Tables available

fact_sales

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Aggregate monthly revenue in a subquery first

Hint 2

Use AVG(monthly_revenue) OVER (ORDER BY year, month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) for the 3-month trailing average

Hint 3

ROWS BETWEEN 2 PRECEDING AND CURRENT ROW = current month + 2 prior months = 3-month window