How to get cumulative sum

前端 未结 16 2545
遇见更好的自我
遇见更好的自我 2020-11-22 03:32
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         


        
16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 04:19

    Try this

    select 
        t.id,
        t.SomeNumt, 
        sum(t.SomeNumt) Over (Order by t.id asc Rows Between Unbounded Preceding and Current Row) as cum
    from 
        @t t 
    group by
        t.id,
        t.SomeNumt
    order by
        t.id asc;
    

提交回复
热议问题