Running Total (Cumulative)
Running Total (Cumulative) is a SQL function in the Window Functions category. Calculates a cumulative sum from the beginning to the current row. The syntax is SUM(column) OVER (ORDER BY ordering_column ROWS UNBOUNDED PRECEDING). It accepts column. It returns cumulative total. A typical example: SELECT transaction_date, amount, SUM(amount) OVER (ORDER BY transaction_date ROWS UNBOUNDED PRECEDING) AS running_balance FROM transactions; -- Cumulative balance over time -- Per account: SELECT account_id, transaction_date, amount, SUM(amount) OVER (PARTITION BY account_id ORDER BY transaction_date) AS account_balance FROM transactions ORDER BY account_id, transaction_date; A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking. A close relative is DENSE_RANK(), which like RANK but without gaps. Equal values get the same rank, and the next number follows sequentially.