18
OctCalculate Running Total, Total of a Column and Row
How to do Running Total: An Overview
You are often required to show information on each transaction and keep a Running Total and Final Total like GridView in Asp.Net. In this SQL Server, I am going to explain, how you can achieve this using SQL Query simply and easily.
SQL Server skills can boost your earning potential by 22% in the next 3 years. Enroll in our Free SQL Server Training Online with Certificate today!
Read More: SQL Server Interview Questions and Answers
Suppose you have the below CustomerOrders table and have the data as shown below:
CREATE TABLE CustomerOrders
(
OrderID int identity,
Amount Decimal(8,2),
OrderDate SmallDatetime default getdate()
)
Go
INSERT INTO CustomerOrders(Amount) Values(120.12)
INSERT INTO CustomerOrders(Amount) Values(20.12)
INSERT INTO CustomerOrders(Amount) Values(10.12)
INSERT INTO CustomerOrders(Amount) Values(30.12)
INSERT INTO CustomerOrders(Amount) Values(40)
GO
SELECT * FROM CustomerOrders
Output
Read More - DBMS Interview Questions For Freshers
Read More - SQL Interview Questions And Answers For Freshers
Read More - DBMS Interview Questions For Freshers
Calculating Running Total
Let's see how to calculate the running total using SQL Query as given below:
select OrderID, OrderDate, CO.Amount
,(select sum(Amount) from CustomerOrders
where OrderID <= CO.OrderID)
'Running Total'
from CustomerOrders CO
Output
Calculating Final Total
Let's see how to calculate the final total using ROLLUP within SQL Query as given below:
SELECT OrderID, SUM(Amount) AS Amount
FROM CustomerOrders
GROUP BY OrderID WITH ROLLUP
Output
Calculating the Total of All Numeric Columns in a Row
Let's see how to calculate the total of all numeric fields within a row using SQL Query as given below:
SELECT OrderID, Amount, SUM(OrderID+Amount) AS RowNumericColSum
FROM CustomerOrders
GROUP BY OrderID,Amount
ORDER BY OrderID
Output
Do you Know?
.NET is gaining popularity day by day, especially after the release of .NET 8. .NET 8 is not only a framework version but much more than that. It redefines the way software applications are built and deployed, enabling developers to meet the evolving demands of modern computing.
.NET full stack developers climb to senior roles 15% faster. Accelerate your growth with our .Net full stack developer course and unlock leadership positions!
Take our Sqlserver skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.