declare @t table
(
id int,
SomeNumt int
)
insert into @t
select 1,10
union
select 2,12
union
select 3,3
union
select 4,15
union
select 5,23
select * from @t
the above select returns me the following.
id SomeNumt
1 10
2 12
3 3
4 15
5 23
How do i get the following
id srome CumSrome
1 10 10
2 12 22
3 3 25
4 15 40
5 23 63
Select *, (Select SUM(SOMENUMT) From @t S Where S.id <= M.id) From @t M
Late answer but showing one more possibility...
Cumulative Sum generation can be more optimized with the
CROSS APPLY
logic.Works better than the
INNER JOIN
&OVER Clause
when analyzed the actual query plan ...Once the table is created -
Without using any type of JOIN cumulative salary for a person fetch by using follow query:
For SQL Server 2012 onwards it could be easy:
because
ORDER BY
clause forSUM
by default meansRANGE UNBOUNDED PRECEDING AND CURRENT ROW
for window frame ("General Remarks" at https://msdn.microsoft.com/en-us/library/ms189461.aspx)Try this