SQL Guide · Prompt: “Calculate cumulative revenue ordered by month from the ledger table”

Calculate a Running Total in SQL Using Window Functions

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Calculate a cumulative running total in PostgreSQL, Snowflake, or BigQuery with SUM() OVER (ORDER BY). Test and convert to Excel/Pandas.

Verified SQL Syntax

SELECT month, revenue,
  SUM(revenue) OVER (ORDER BY month) AS running_total
FROM ledger;

How This Formula Works (Step-by-Step)

Window functions let you compute running totals without collapsing individual rows like GROUP BY does.

Step 1: Keep your row-level columns (month, revenue) in SELECT.

Step 2: Add SUM(revenue) OVER (ORDER BY month) to accumulate values sequentially in chronological order.

Formula Syntax & Arguments Breakdown

Applies SUM(metric) OVER (ORDER BY sort_col) as a non-collapsing window function so every original row stays intact alongside its cumulative sum.

Argument / ClauseRequirementData TypeRole & Behavior
SUM(revenue)RequiredAggregate FunctionNumeric metric accumulated across the ordered window frame.
OVER (...)RequiredWindow SpecificationTransforms SUM from a table-collapsing aggregate into a row-preserving window calculation.
PARTITION BY account / ORDER BY monthRequiredWindow ClausesORDER BY sets accumulation chronology; adding PARTITION BY resets the running total per account or year.

3 Real-World Use Cases

  • Generating cumulative SaaS ARR curves for board reporting.
  • Tracking rolling user signups since product launch.
  • Computing running inventory balances across warehouse movements.

Cross-Platform Equivalents

Excel
=SUM($C$2:C2)
SQL
SELECT month, revenue, SUM(revenue) OVER (ORDER BY month) AS running_total FROM ledger;
Python Pandas
df['running_total'] = df.sort_values('month')['revenue'].cumsum()

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: When two rows share the exact same ORDER BY date/month, SQL default RANGE frame sums both tied rows together on both lines.

    Fix: Add a deterministic tiebreaker (ORDER BY month, id) or specify ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

  • Mistake: Sorting dates stored as text ("10-01", "02-01") scrambles cumulative running totals alphabetically.

    Fix: Always order by an ISO date or true DATE/TIMESTAMP column.

  • Mistake: Filtering a window function directly in WHERE fails because window functions evaluate after WHERE and GROUP BY.

    Fix: Wrap the query inside a CTE or subquery first, then filter running_total in the outer SELECT.

Engineering Pro Tips

  • Pre-aggregate daily transactions inside a monthly CTE before running SUM(...) OVER (ORDER BY month) for clean time-series charts.
  • Replace SUM with AVG(...) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) to build a 3-period moving average.
  • Add PARTITION BY EXTRACT(YEAR FROM month) to automatically reset cumulative YTD totals each January.

Frequently Asked Questions

How do I restart the running total for each region or account?
Add PARTITION BY inside OVER: SUM(revenue) OVER (PARTITION BY region ORDER BY month).
What happens if two rows share the exact same date?
Standard SQL treats peers together under RANGE; add a unique ID to ORDER BY (ORDER BY month, id) for strict row-by-row accumulation.
Which SQL databases support SUM() OVER?
PostgreSQL, MySQL 8+, SQLite 3.25+, BigQuery, Snowflake, Redshift, and SQL Server all support it.

Related Formulas & Guides