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.

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 →