SQL SERVER 2012 HOSTING :: How to Find Running Total in SQL Server

Finding running total in SQL Server

Finding running total is one of the most popular request user encounters in the industry. There are two different ways to find out running totals. One of the methods is as per SQL Server 2008 R2 and earlier version. This is indeed a very expensive version of finding running total and I always hated this solution when I had to implement it in the industry. However, I am extremely delighted since SQL Server 2012 as it has a new feature of OVER ORDER BY ROW methods. It is much more efficient and cleaner to implement.

Let us first create a sample table and populate the same

Here is the screenshot of the resultset.

runningtotal1

Here is the query which you can execute on SQL Server 2008 R2 or earlier version. The query is very expensive.

Here is the query which you can execute on SQL Server 2012 or later version. The query is very efficient.

Both of the above query returns following results.

runningtotal2

If there is any other better option, please share it here 🙂 tnx ..