A running total continuously adds values in a defined order while retaining each individual transaction row.
Cumulative values help analyze financial growth, sales progression, and account balances over time.
Show each customer order with a cumulative sales total.
SELECT
c.CustomerName,
o.OrderDate,
o.OrderAmount,
SUM(o.OrderAmount) OVER (
PARTITION BY o.CustomerID
ORDER BY o.OrderDate, o.OrderID
ROWS UNBOUNDED PRECEDING
) AS RunningSales
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;
Used in financial statements, sales dashboards, account balances, and performance tracking.
- ORDER BY defines the running sequence.
- PARTITION BY creates separate customer totals.
- ROWS UNBOUNDED PRECEDING starts from the first row.
- A tie-breaker can make ordering deterministic.
- Useful for time-based analysis.
How do you calculate a running total?
Use a SUM window function with an ORDER BY clause and an appropriate window frame.
Why include OrderID in the ORDER BY?
It provides a deterministic order when multiple transactions have the same date.