| Author |
Topic |
|
Rosembm1
Starting Member
15 Posts |
Posted - 2010-01-04 : 08:24:02
|
| I am trying to create a running total by character group: The table setup is below: create table #Temp (GroupA varchar(255),GroupB varchar(255),TotalB smallint) insert into #temp VALUES ('Hip','Path1', 2) insert into #temp VALUES ('Hip','Path2', 2) insert into #temp VALUES ('Hip','Path3 ', 2) insert into #temp VALUES ('Hip','Path4', 1) insert into #temp VALUES ('Hip','Path5', 1) insert into #temp VALUES ('Knee', 'Path1', 4) insert into #temp VALUES ('Knee', 'Path2', 3) insert into #temp VALUES ('Knee' , 'Path3', 3) insert into #temp VALUES ('Knee', 'Path4', 3) insert into #temp VALUES ('Knee', 'Path5', 1) insert into #temp VALUES ('Knee' , 'Path6', 1) insert into #temp VALUES ('Knee', 'Path7', 1) insert into #temp VALUES ('Knee', 'Path8', 1) insert into #temp VALUES ('Knee' ,'Path9', 1)The output I am seeking is in the far right columnGroupA GroupB TotalB Running_TotalHip Procedure Path1 2 2Hip Procedure Path2 2 4Hip Procedure Path3 2 6Hip Procedure Path4 1 7Hip Procedure Path5 1 8Knee Procedure Path1 4 4Knee Procedure Path2 3 7Knee Procedure Path3 3 10Knee Procedure Path4 3 13Knee Procedure Path5 1 14Knee Procedure Path6 1 15Knee Procedure Path7 1 16Knee Procedure Path8 1 17Knee Procedure Path9 1 18Thanks for your help!MWRosenblattSpins Yak |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-04 : 08:51:26
|
Which version of SQL Server? No, you're never too old to Yak'n'Roll if you're too young to die.Edit: Ignore my question. I thought about sum() over (partition by...) but that isn't the answer...sorry.Look here:http://www.sqlteam.com/article/calculating-running-totalsand here:http://weblogs.sqlteam.com/mladenp/archive/2009/07/28/SQL-Server-2005-Fast-Running-Totals.aspx |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-01-04 : 09:27:52
|
| <<sum() over (partition by...) >>That was not yet supported but very usefulMadhivananFailing to plan is Planning to fail |
 |
|
|
Rosembm1
Starting Member
15 Posts |
Posted - 2010-01-04 : 11:00:52
|
| We are currently using SQL server 2005. The first thing I had tried the sum over partition by and I could not get it to work.MWRosenblattSpins Yak |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-04 : 11:12:50
|
Try this..SELECT a.GroupA, a.GroupB, SUM(b.TotalB) AS [RunningTotal]FROM (select row_number() over(partition by GroupA order by GroupB) as seq,* from #temp) aCROSS JOIN (select row_number() over(partition by GroupA order by GroupB) as seq,* from #temp) bWHERE (b.seq <= a.seq and b.GroupA = a.GroupA) GROUP BY a.GroupA,a.GroupBORDER BY a.GroupA,a.GroupB |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-04 : 11:13:36
|
Or this...whichever is fasterSELECT GroupA, GroupB, TotalB+COALESCE((SELECT SUM(TotalB) FROM (select row_number() over(partition by GroupA order by GroupB) as seq,* from #temp) b WHERE b.seq < a.seq and a.GroupA = b.GroupA),0) AS RunningTotalFROM (select row_number() over(partition by GroupA order by GroupB) as seq,* from #temp) aORDER BY GroupA,GroupB |
 |
|
|
Rosembm1
Starting Member
15 Posts |
Posted - 2010-01-04 : 12:57:22
|
| Thanks! MMWRosenblattSpins Yak |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-04 : 13:01:26
|
| You're welcome. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Sachin.Nand
2937 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|