Services

Technologies

Industries

About Us

Our Work - Case Studies

Running Totals in SQL Server

Running Totals in SQL Server

How to Calculate Running Totals in SQL Server

Calculating running totals (cumulative sums) is a fundamental technique in SQL Server. Whether you are tracking daily sales performance, monitoring stock levels, or building financial ledger reports, running totals give you a clear view of cumulative progress over time.

Looking to Optimise Your SQL Server Performance?

While basic window functions are simple to write, calculating running totals over millions of rows can significantly impact query speed.

Our SQL Server Consulting Services can help you architect high-performance indexing strategies, optimise complex window logic, and build scalable reporting data models.

Learn More: SQL Server Consulting

What is a Running Total?

A running total is a cumulative sum that adds each new value in a sequence to the running total of all previous values.

In data analysis and business intelligence, running totals are commonly used to calculate cumulative figures over a given period, such as tracking year-to-date revenue, cumulative web conversions, or rolling account balances.

Example Use Case: Inventory Forecasting

A practical business example is predicting closing inventory levels.

By calculating a running total of daily predicted sales alongside expected incoming supplier shipments, inventory planners can immediately identify potential stock-outs or overstock scenarios before they occur:

Starting Stock + Cumulative Deliveries – Cumulative Sales = Predicted Stock Balance
 

Why Calculate a Running Total?

Calculating running totals is an essential technique in financial analysis, operational reporting, and forecasting. Instead of evaluating isolated daily or transactional figures, cumulative totals allow you to track overall trajectory, velocity, and performance trends over time.
 
Key business applications include:
 
  • Financial Reporting: Monitoring year-to-date (YTD) revenue, running cash balances, and budget consumption.

  • Sales & Performance Tracking: Evaluating cumulative target progression across months, quarters, or campaigns.

  • Inventory & Supply Chain: Predicting stock depletion rates to identify potential stock-outs early. 

Returning to the inventory forecasting example, tracking running balances enables supply chain teams to highlight impending stock shortages early. This provides actionable visibility to schedule replenishment deliveries at precisely the right time, preventing out-of-stock scenarios without holding excess capital in safety stock. For large-scale operational environments with millions of transactions, integrating these calculations directly into your Data Warehouse ensures reporting stays fast and responsive.

 
 

How to Calculate a Running Total in SQL Server

Let’s walk through a practical example step-by-step.

Suppose you have a table named [dbo].[SalesSample] containing daily sales records:

Daily Sales example table in SQL
 
 

Calculating the Running Total with Window Functions

To see the cumulative total of sales at the end of each day, SQL window functions make the query straightforward and elegant.

By combining the SUM() aggregate function with an OVER (ORDER BY ...) clause, SQL Server calculates a cumulative total for each row sequentially:

SQL snippet showing Running Totals in SQL Server
 
 

Syntax Breakdown: How the Window Function Works

step by step breakdown of SQL code to generate running totals
 

1. SUM(Sales_Gross)

Specifies the aggregation method and target measure. While running totals most commonly use SUM(), you can also swap this for AVG(), COUNT(), MIN(), or MAX() to calculate running averages or moving extrema.

2. OVER (ORDER BY SaleDate ASC)

Defines the window frame and sequencing logic. The ORDER BY clause instructs SQL Server to order rows chronologically before applying the cumulative sum, ensuring each row includes all preceding rows up to that point.

3. AS [Running Total]

Assigns a clear, descriptive column alias to the resulting output field.

You can now calculate cumulative running totals in SQL Server using this window function pattern.
 

By swapping out SUM(), you can easily adapt this logic to calculate other rolling calculations:

  • Running Count: COUNT(OrderID) OVER (ORDER BY SaleDate ASC) to track transaction growth over time.

  • Running Average: AVG(Sales_Gross) OVER (ORDER BY SaleDate ASC) to smooth out daily fluctuations.

  • Moving Extrema: MAX(Sales_Gross) or MIN(Sales_Gross) to track historical peaks and troughs.

 

Conclusion

Running totals are an essential technique for data analysis, financial accounting, and inventory forecasting. They allow you to track cumulative trends over time and see historical balances at any given point.

Calculating running totals in SQL Server is straightforward using the SUM() aggregate function combined with the OVER (ORDER BY ...) window clause. To calculate separate running totals across categories, such as by department, region, or product line, simply add a PARTITION BY clause.

Subscribe for More Data & Analytics Tips

Watch short video tutorials, SQL walkthroughs, and Power BI timesavers on the Select Distinct YouTube channel.

Picture of Simon Harrison

Simon Harrison

Simon Harrison is the founder and director of Select Distinct Limited and a Business Intelligence expert.
He specialises in SQL Server architecture, Power BI, and enterprise data warehousing.