How to get cumulative sum

前端 未结 16 2373
遇见更好的自我
遇见更好的自我 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:14

    There is a much faster CTE implementation available in this excellent post: http://weblogs.sqlteam.com/mladenp/archive/2009/07/28/SQL-Server-2005-Fast-Running-Totals.aspx

    The problem in this thread can be expressed like this:

        DECLARE @RT INT
        SELECT @RT = 0
    
        ;
        WITH  abcd
                AS ( SELECT TOP 100 percent
                            id
                           ,SomeNumt
                           ,MySum
                           order by id
                   )
          update abcd
          set @RT = MySum = @RT + SomeNumt
          output inserted.*
    

提交回复
热议问题