February 2, 2012 at 6:15 am
TableStructure
Count Length
1 10
2 5
3 5
4 7
I need the Result Set like without using the loop logics in sql server.
Count Length T.Length
1 10 10
2 5 15 (ie.10+5 Length+ Prev. T.Lenght)
3 5 20 (ie.15+5 Length+ Prev. T.Lenght)
4 7 27 (ie.20+7 Length+ Prev. T.Lenght)
February 2, 2012 at 6:35 am
nature_cbe (2/2/2012)
TableStructureCount Length
1 10
2 5
3 5
4 7
I need the Result Set like without using the loop logics in sql server.
Count Length T.Length
1 10 10
2 5 15 (ie.10+5 Length+ Prev. T.Lenght)
3 5 20 (ie.15+5 Length+ Prev. T.Lenght)
4 7 27 (ie.20+7 Length+ Prev. T.Lenght)
You can find a couple of solutions by searching for TSQL RUNNING TOTALS
declare @atable table ([count] int, [length] int);
insert into @atable
values (1, 10), (2, 5), (3, 5), (4,7);
SELECT a.[count], a.[length],
(SELECT SUM(length) FROM @atable WHERE [COUNT] <= a.[count]) as TLength
FROM @atable a
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply