Calculate Running Total, Total of a Column and Row

Use window functions to build running totals and summary rows directly in SQL.

Reports often need more than a single grand total — they need a running total that accumulates row by row, alongside column and row-level summaries. SQL Server's window functions make this possible without writing a single loop.

Running Total with SUM() OVER()

SELECT order_date, amount,
  SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM sales_orders;

The OVER (ORDER BY ...) clause tells SQL Server to keep adding each row's value to the ones before it, producing a cumulative total that grows down the result set.

Running Total Per Group

SELECT region, order_date, amount,
  SUM(amount) OVER (PARTITION BY region ORDER BY order_date) AS region_running_total
FROM sales_orders;

Adding PARTITION BY region restarts the running total separately for each region.

Total of a Column

SELECT SUM(amount) AS total_sales FROM sales_orders;

Total of a Row (Across Multiple Columns)

SELECT product_name,
  (jan_sales + feb_sales + mar_sales) AS quarter_total
FROM monthly_sales;
💡 Tip: Combine SUM() OVER() with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW for full control over exactly which rows are included in the running calculation.

Where This Is Used

  • Sales dashboards showing cumulative revenue over time
  • Bank statement style running balances
  • Finance reports comparing per-row totals against overall column totals

Window functions like these replace what used to require cursors or client-side loops, keeping the calculation fast and entirely inside the database.

Running Totals in MySQL and the Tie Problem

The same window-function syntax works in MySQL 8.0 and later. One detail surprises many people: when OVER has an ORDER BY but no frame, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With RANGE, rows that share the same sort value are treated as one group and receive the same total.

SELECT id, week_no, amount,
       SUM(amount) OVER (ORDER BY week_no) AS range_total,
       SUM(amount) OVER (ORDER BY week_no, id
                         ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rows_total
FROM weekly_sales;
idweek_noamountrange_totalrows_total
11500500500
223001000800
3220010001000
4340014001400

Both columns end at the same grand total, but only rows_total grows one row at a time. For statements and ledgers, use ROWS and add a unique column such as id to the ORDER BY so the order is always predictable.

Grand Totals, Percentages and Moving Sums

To add a total row under grouped results, MySQL uses WITH ROLLUP. The GROUPING() function tells you which row is the summary, so a real NULL region is not mislabelled:

SELECT IF(GROUPING(region), 'All regions', region) AS region,
       SUM(amount) AS total
FROM sales_orders
GROUP BY region WITH ROLLUP;

In SQL Server, the equivalent is GROUP BY ROLLUP(region), and it also supports GROUPING().

An empty OVER () gives every row access to the overall total, which makes share-of-total calculations easy:

SELECT region, amount,
       ROUND(100 * amount / SUM(amount) OVER (), 2) AS pct_of_total
FROM sales_orders;

A moving total over the last three rows smooths out spikes in a trend chart:

SELECT id, week_no, amount,
       SUM(amount) OVER (ORDER BY week_no, id
                         ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS last_3_total
FROM weekly_sales;

Row Totals and Older MySQL Versions

When adding columns across a row, remember that any NULL makes the whole sum NULL. Wrap each column in COALESCE:

SELECT product_name,
       COALESCE(jan_sales, 0) + COALESCE(feb_sales, 0) + COALESCE(mar_sales, 0) AS quarter_total
FROM monthly_sales;

MySQL 5.7 has no window functions. A correlated subquery gives the same running total, although it gets slow on large tables because it re-scans earlier rows for each row:

SELECT o.id, o.amount,
       (SELECT SUM(i.amount) FROM sales_orders i WHERE i.id <= o.id) AS running_total
FROM sales_orders o
ORDER BY o.id;

If you are still on 5.7, upgrading is usually the better long-term fix than tuning workarounds like this one.

Mistakes to Check Before Publishing a Report

  • Leaving out a tie-breaker in ORDER BY, so rows with the same date total differently between runs.
  • Forgetting PARTITION BY when each customer or account needs its own running balance.
  • Filtering in an outer query after the window is computed, then wondering why totals include hidden rows.
  • Integer division in percentages on some databases; multiply by 100.0 to keep decimals where needed.

An index that matches the partition and order columns, such as (region, order_date), helps the database read rows in the right order and keeps these queries fast.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →