sqlcmd.net validated sql reference
advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Calculate A Running Total With SUM OVER

Accumulate a column's values row by row using `SUM` as a window function.

Docker-validated Not currently validation-green

Show the cumulative revenue total for each day

Each row's running_total is the sum of all amounts from the first row through the current row in date order. Jan 1 starts at 100; Jan 2 adds 150 to reach 250; Jan 3 adds 80 to reach 330; Jan 4 adds 200 to reach 530. Add PARTITION BY category before ORDER BY to reset the running total for each category.

Rows loaded before the example query runs.
Setup
CREATE TABLE sales (id INT, sale_date VARCHAR(10), amount INT);

INSERT INTO
  sales (id, sale_date, amount)
VALUES
  (1, '2024-01-01', 100),
  (2, '2024-01-02', 150),
  (3, '2024-01-03', 80),
  (4, '2024-01-04', 200);
Shared across supported engines.
SQL
SELECT
  sale_date,
  amount,
  SUM(amount) OVER (
    ORDER BY
      sale_date
  ) AS running_total
FROM
  sales
ORDER BY
  sale_date;
Returned rows for the shared example.
sale_dateamountrunning_total
2024-01-01100100
2024-01-02150250
2024-01-0380330
2024-01-04200530

Output is identical across all supported engine versions.

Where this command helps.

  • accumulating a column value progressively across ordered rows
  • computing a daily or monthly running balance

What the command is doing.

SUM(column) OVER (ORDER BY ...) computes a cumulative sum: for each row, it adds all values from the first row up to and including the current row, in the specified order. Unlike GROUP BY, the original rows are preserved. This is the standard pattern for running totals, balance calculations, and cumulative metrics.