CODE WORKSPACE

Revenue waterfall reconstruction

Write a query that reconstructs monthly revenue movement by separating new, expansion, contraction, and churn amounts into a readable waterfall.

Start with one CTE that groups monthly movements, then project net revenue change from the named movement columns.

Sample input

revenue_eventsMonthly account-level revenue movements used to rebuild the waterfall.
account_idevent_monthmovement_typeamount
2012026-01new1200
2022026-01new800
2012026-02expansion300
2022026-02contraction-150
2032026-03churn-500

Expected output

Expected output shapeExample monthly waterfall rows. Your final query should return all months from the sandbox.
event_monthnew_revenueexpansion_revenuecontraction_revenuechurn_revenuenet_revenue_change
2026-0120000002000
2026-02500300-1500650
2026-03900200-100-500500

Constraints

Preserve the sign of contraction and churn movements, keep each movement type visible, and return one row per month ordered chronologically.

Expected skills

Conditional aggregation, clear CTE naming, financial sign handling, and explaining tradeoffs in revenue movement logic.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.