Coding question · Running TotalMediumAsked at Twitter

Running total of monthly revenue

Window function: running SUM over a date bucket.

Back to all coding questions

The Brief

Finance AnalyticsCFO's office#coding-interview

For each month (YYYY-MM), return total monthly revenue AND the running total of revenue from the beginning of the data up through that month. Columns: `month`, `monthly_revenue`, `running_total`. Order by month. Table: `ci_orders`.

Tables & columns available

ci_ordersfact6 columns
ColumnTypeKey
order_idINTPK
customer_idINT
product_nameTEXT
categoryTEXT
order_dateDATE
revenueNUMERIC

Hints (2)

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

Hint 1

TO_CHAR(DATE_TRUNC('month', order_date), 'YYYY-MM') for the month label.

Hint 2

SUM(SUM(revenue)) OVER (ORDER BY DATE_TRUNC('month', order_date)) for the running total.