“`html
Computing the Running total in SQL Server is vital for cumulative sum calculations in reports, financial assessments, and monitoring trends. Summing values row by row according to a defined order, like transaction IDs, aids in preserving an accurate cumulative total. In this blog, we will delve into the various techniques for determining the running total in SQL Server, complete with examples for each method.
Table of Contents:
- Why is it important to calculate the SQL Server Running Total?
- Techniques to Compute the Running Total in SQL Server
- Different Methods for Calculating the Running Total
- Comparison of Performance Across Methods
- Practical Examples
- Recommended Practices
- Summary
Why is it important to calculate the SQL Server Running Total?
Calculating the running total in SQL Server facilitates the identification of patterns and trends within the dataset, aiding in decision-making by allowing the monitoring of accumulated totals over time. These principles are predominantly employed in business intelligence and performance evaluation to juxtapose calculated figures against established targets. Implementing SQL-based solutions guarantees effective, scalable, and real-time calculations within the database.
Techniques to Compute the Running Total in SQL Server
Prior to diving into the various techniques, let’s set up a Transactional Log table and populate it with values for demonstration purposes in the upcoming methods.
CREATE TABLE Tran_Log (
Tran_ID INT PRIMARY KEY IDENTITY(1,1),
EventSeq INT,
Tran_Value DECIMAL(10,2)
);
INSERT INTO Tran_Log (EventSeq, Tran_Value) VALUES
(1, 120.00),
(2, 180.50),
(3, 210.75),
(4, 260.00),
(5, 300.25);
SELECT * FROM Tran_Log;

This illustrates the table’s appearance once created and populated with values.
Technique 1: Utilizing SUM() with OVER(ORDER BY) Clause in SQL Server
One of the most efficient methods for determining a running total involves the use of Window functions, which eliminate the need for subqueries or JOINS.
Syntax:
SELECT
col1,
col2,
SUM(aggregate) OVER (ORDER BY colname)
FROM table;
Example:
SELECT
Tran_ID,
EventSeq,
Tran_Value,
SUM(Tran_Value) OVER (ORDER BY EventSeq) AS AccumulatedValue
FROM Tran_Log;
Output:

Explanation: In this case, the cumulative total is calculated sequentially from row to row using the SUM(Tran_Value) OVER (ORDER BY EventSeq) function.
Technique 2: Utilizing Correlated Subquery in SQL Server
A correlated subquery computes the running total based on all preceding values within the dataset prior to the current record.
Syntax:
SELECT
main_table.column1,
main_table.column2,
(SELECT SUM(sub_table.aggregate_column)
FROM table_name sub_table
WHERE sub_table.order_column <= main_table.order_column)
FROM table_name main_table
ORDER BY main_table.order_column;
Example:
SELECT
TL_A.Tran_ID,
TL_A.EventSeq,
TL_A.Tran_Value,
(
SELECT SUM(TL_B.Tran_Value)
FROM Tran_Log TL_B
WHERE TL_B.EventSeq <= TL_A.EventSeq
) AS AggregatedTotal
FROM Tran_Log TL_A
ORDER BY TL_A.EventSeq;
Output:

Explanation: In this instance, the subquery executes for each row in TL_A, summing all transactional values where TL_B.EventSeq is less than or equal to TL_A.EventSeq.
Technique 3: Utilizing CTE with SUM() in SQL Server
A Common Table Expression (CTE) serves as a temporary result set formatted logically. Instead of relying on a subquery for each row, we can derive the total by using a recursive CTE combined with the SUM() function.
Syntax:
WITH CTE AS (
SELECT
colA,
colB,
SUM(column) OVER (ORDER BY colm_name) AS Total
FROM table_name
)
SELECT * FROM CTE;
Example:
WITH EventTotals AS (
SELECT
Tran_ID,
EventSeq,
Tran_Value,
SUM(Tran_Value) OVER (ORDER BY EventSeq) AS CTERunningTotal
FROM ``````html Tran_Log
)
SELECT * FROM EventTotals;
Result:

Clarification: In this context, the CTE retains interim results, while the SUM(Tran_Value) OVER (ORDER BY EventSeq) function guarantees that each row sums up all preceding rows for display.
Method 4: Leveraging Variables in the SELECT Statement within SQL Server
Utilizing Variables in the SELECT Statement allows for a variable assignment throughout the execution process that consistently updates as each row is sequentially handled.
Formula:
DECLARE @RunningTotal DECIMAL(10,2) = 0;
SELECT
column1,
column2,
SET @RunningTotal = @RunningTotal + aggregate_column
FROM table_name
ORDER BY order_column;
Illustration:
DECLARE @CumulativeSum DECIMAL(10,2) = 0;
SELECT
Tran_ID,
EventSeq,
Tran_Value,
RunningTotal = CAST(0 AS DECIMAL(10,2))
INTO #TempRunningTotal
FROM Tran_Log
ORDER BY EventSeq;
UPDATE #TempRunningTotal
SET @CumulativeSum = RunningTotal = @CumulativeSum + Tran_Value;
SELECT * FROM #TempRunningTotal;
DROP TABLE #TempRunningTotal;
Result:

Clarification: In this scenario, the @CumulativeSum variable is defined to hold the running total, which is updated for each row by adding SalesAmount through the UPDATE Statement.
Alternate Methods for Determining the Running Total
There are several techniques to calculate the running total, including the utilization of Window functions in MySQL or employing a Recursive CTE in SQLite.
Method 1: Utilizing Window Functions in MySQL
This approach employs Window functions to derive cumulative sums without the necessity for subqueries or JOINS.
Formula:
SELECT
col1,
col2,
SUM(aggregate) OVER (ORDER BY colname)
FROM table;
Illustration:
CREATE TABLE Tran_Log (
Tran_ID INT PRIMARY KEY AUTO_INCREMENT,
EventSeq INT,
Tran_Value DECIMAL(10,2)
);
INSERT INTO Tran_Log (EventSeq, Tran_Value) VALUES
(1, 130.00),
(2, 150.50),
(3, 280.75),
(4, 278.00),
(5, 301.25);
SELECT
EventSeq,
Tran_Value,
SUM(Tran_Value) OVER (ORDER BY EventSeq) AS Running_Total
FROM Tran_Log;
Result:

Clarification: Here, the cumulative total is computed from row to row with the usage of the SUM(Tran_Value) OVER (ORDER BY EventSeq) function.
Method 2: Applying Recursive CTE in SQLite
A recursive common table expression (CTE), which allows multiple references to the same CTE, proves advantageous for calculations that incorporate running totals.
Formula:
WITH RECURSIVE CTE AS (
SELECT KeyID, Value
FROM YrTab
WHERE KeyID = (SELECT MIN(KeyID) FROM YourTable)
UNION ALL
SELECT yt.KeyID, yt.Value, pr.Cumulative + yt.Value
FROM YrTab yt
JOIN pr ON yt.KeyID = pr.KeyID + 1
)
SELECT * FROM tab_name;
Illustration:
CREATE TABLE Tran_Log (
Tran_ID INTEGER PRIMARY KEY AUTOINCREMENT,
EventSeq INTEGER,
Tran_Value DECIMAL(10,2)
);
INSERT INTO Tran_Log (EventSeq, Tran_Value) VALUES
(1, 120.00),
(2, 180.50),
(3, 210.75),
(4, 260.00),
(5, 300.25);
WITH RECURSIVE Log_Tracker AS (
SELECT Tran_ID, EventSeq, Tran_Value, Tran_Value AS RunningTotal
FROM Tran_Log
WHERE Tran_ID = (SELECT MIN(Tran_ID) FROM Tran_Log)
UNION ALL
SELECT t.Tran_ID, t.EventSeq, t.Tran_Value, lt.RunningTotal + t.Tran_Value
FROM Tran_Log t
JOIN Log_Tracker lt ON t.Tran_ID = lt.Tran_ID + 1
)
SELECT * FROM Log_Tracker;
Result:

Clarification: Start with the earliest transaction using MIN(Tran_ID) to calculate the running total, then recursively add each subsequent Tran_Value to the last total through the join on Tran_ID +1.
Performance Assessment of Each Technique
Technique | Application Scenario | Advantages | Disadvantages |
---|---|---|---|
SUM() with OVER (ORDER BY) | Best for sequential datasets in substantial datasets | Advanced for accumulating totals | Incompatible with earlier versions of SQL |
Correlated Subquery | Applicable while dealing with smaller datasets | Straightforward to implement | Suboptimal performance due to row-by-row processing |
CTE with SUM() | Beneficial where further transformations are required | Simple to understand and execute | CTEs could compromise performance if materialized repeatedly in extensive queries |
Variables in the SELECT statement | Effectively functions when the sequence is established | Processes smaller datasets effectively | Does not accommodate parallel execution |
Practical Instances
1. Expense Monitoring System
An organization aims to monitor the total expenditures over a period.
Illustration:
CREATE TABLE Bud_Spend (
DeptID INT,
SpendDt DATE,
AmountSpent DECIMAL(10,2)
);
INSERT INTO Bud_Spend VALUES
(1, '2024-01-10', 1000),
(1, '2024-03-15', 1500),
(1, '2024-05-20', 1300),
(2, '2024-02-05', 2000),
(2, '2024-04-18', 1800),
(2, '2024-06-25', 1600);
SELECT
DeptID,
SpendDt,
AmountSpent,
SUM(AmountSpent) OVER (PARTITION BY DeptID ORDER BY SpendDt) AS TotalSpend
FROM Bud_Spend;
Result:
“`

Clarification: In this instance, the cumulative amount expended can be computed based on the SpendDt by utilizing the SUM(AmountSpent) OVER (PARTITION BY DeptID ORDER BY SpendDt) function.
2. Score Card Monitoring
An educational institution intends to monitor the performance scores of a participant in a cricket match.
Illustration:
CREATE TABLE Play_Sco (
Ply_ID INT,
Ply_Dt DATE,
Score INT
);
INSERT INTO Play_Sco VALUES
(10, '2024-01-05', 50),
(10, '2024-01-10', 70),
(10, '2024-01-15', 80),
(11, '2024-02-01', 60),
(11, '2024-02-08', 90),
(11, '2024-02-15', 75);
SELECT
Ply_ID,
Ply_Dt,
Score,
SUM(Score) OVER (PARTITION BY Ply_ID ORDER BY Ply_Dt) AS RunningScore
FROM Play_Sco;
Result:

Clarification:In this example, the total score of a participant is determined using the SUM(Score) OVER (PARTITION BY Ply_ID ORDER BY Ply_Dt) function.
Optimal Practices
- Utilize window functions: Window functions enhance execution speed as they are more efficient compared to correlated subqueries and cursors.
- Steer Clear of Cursors with Extensive Data Sets: Cursors process each row individually, leading to reduced performance. It is advisable to avoid cursors with large datasets; instead, consider set-based methods, including CTE or window functions.
- Implement Proper Indexing: Adding an index to the columns can significantly improve performance, particularly with large datasets.
Final Thoughts
The most proficient approach to compute the total is by employing the Windows function; however, based on the specific scenario and performance needs, alternative strategies such as CTE, Correlated subqueries, and variables can also be adopted. Throughout this article, you have acquired insights into various techniques to compute the running total in SQL Server.
Enhance your expertise by signing up for our SQL Training Course today to gain practical experience. Moreover, get ready for job interviews with our SQL interview queries, curated by industry professionals.
Calculate Running Total in SQL Server – Frequently Asked Questions
The total of a sequence of numbers is referred to as a running total, and it is updated as each new number is introduced by adding its value to the existing running total.
The cumulative sum of the numerical fields is returned through the SUM() function.
SUM() OVER(ORDER BY Column) is the most prevalent and effective technique.
Yes, there are several alternative methods, such as utilizing CTE WITH SUM(), variables within the SELECT statement, and associated subqueries.
Processing extensive datasets is slower because the cursor assesses each row one at a time.
The article Calculate Running Total in SQL Server first appeared on Intellipaat Blog.