Coding question · Running TotalHardAsked at Meta

3-month rolling average revenue

Window function with a ROWS BETWEEN frame.

Back to all coding questions

The Brief

Finance AnalyticsFP&A team#coding-interview

For each month (YYYY-MM), return total monthly revenue AND a 3-month trailing average (current month plus the 2 prior; at the left edge the average just uses what's available). Columns: `month`, `monthly_revenue`, `rolling_3mo_avg`. Round the average to 2 decimals. Order by month. Table: `ci_orders`.

Tables & columns available

ci_ordersfact6 columns
ColumnTypeKey
order_idINTPK
customer_idINT
product_nameTEXT
categoryTEXT
order_dateDATE
revenueNUMERIC

Hints (3)

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

Hint 1

Bucket with DATE_TRUNC('month', order_date) in a CTE.

Hint 2

AVG(monthly_revenue) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW).

Hint 3

ROUND(..., 2) to match the expected decimal format.