Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Can you accumulate a value in a singe query, so that each row has the sum of all rows "above" it? For example, assume my table is;Name Score AccScore----- ----- --------Abbot 10 0Barns 5 0Cabot 12 0And I want this:Name Score AccScore----- ----- --------Abbot 10 10Barns 5 15Cabot 12 27I always end up writing a program in VB to do this.Dom
Stalker
Yak Posting Veteran
80 Posts
Posted - 2005-07-28 : 12:30:40
using cursors you can do this easily without VB
Kristen
Test
22859 Posts
Posted - 2005-07-28 : 12:47:43
Dunno if this would work - cursor will be slow
SELECT A.name, A.score, B.AccScoreFROM MyTable A JOIN ( SELECT B.Name, [AccScore] = SUM(B.Score) FROM MyTable B WHERE B.Name <= A.Name GROUP BY B.Name ) B ON B.Name = A.NameORDER BY A.Name