Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Running Total

Author  Topic 

ConfusedAgain
Yak Posting Veteran

82 Posts

Posted - 2011-04-08 : 04:42:42
How do I create a running total of a field in a view?

Record Value RunningTotal
1 50 50
2 75 125
3 25 150



cns1202
Starting Member

7 Posts

Posted - 2011-04-08 : 05:14:23
select * from(
select REcord,Value, (select sum(Value) from ab where Record<=T.Record) as 'RunningTotal'
from ab T) M

ab - your TableName

--had faced a similar situation before, and Madhivan's blog post helped me..hope it helps you too :)

Regards,
Chirag Shah
Go to Top of Page

ConfusedAgain
Yak Posting Veteran

82 Posts

Posted - 2011-04-08 : 05:28:38
Spot on - excellant works a treat.

many thanks.
Go to Top of Page

theboyholty
Posting Yak Master

226 Posts

Posted - 2011-04-08 : 06:07:10
A man who answers a question that has already been answered is a man with too much time on his hands.

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-04-08 : 15:57:24
That view will be very, very slow. Better calculate running total on the client.

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-04-09 : 11:50:57
the <= approach is poor for many rows in the tabgle.

better read this: http://www.sqlservercentral.com/articles/T-SQL/68467/


elsasoft.org
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2011-04-16 : 20:28:43
I realize the question has already been answered and I realize the proper warning about performance of that answer has been given. My question is, how many rows are we talking about here?


--Jeff Moden
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-17 : 11:17:00
If there are only a few hundreds (or few thousands?) of rows, and if the calculations need to be done only infrequently, I guess it wouldn't matter. But the increase in resources and computation is O(2) or nearly so, which means that for most other scenarios, the approach shown above probably would not be suitable.

Other approaches I can think of (other than doing it on the client - which I am not sure is always possible or even if possible is the best approach) are:

1. Use a cross-join. Performance unlikely to be any better.
2. Use cross-apply. Probably same poor performance.
3. Use a recursive CTE - shown below - should perform better.

WITH CTE AS
(
SELECT Record, value, value as RunningTotal FROM ab WHERE Record = 1
UNION ALL
SELECT a.Record, a.value, c.RunningTotal + a.value
FROM ab a INNER JOIN CTE c ON c.Record+1 = r.Record
)
SELECT * FROM CTE OPTION (MAXRECURSION 0);
This assumes that data in the Record column has no gaps; if not a preceding CTE that generates consecutive row_numbers would be required.
Go to Top of Page
   

- Advertisement -