declare @t table(A varchar(10),B int)
insert into @t select 'a',1
insert into @t select 'a',2
insert into @t select 'a',3
insert into @t select 'b',20
insert into @t select 'b',9
insert into @t select 'c',9
insert into @t select 'c',6declare @a varchar(10),@b int
update @t
set
    @b=case when A!=isnull(@a,'') then B else B+@b end,
    @a=A,
    B = @bselect * from @t
/*
A          B           
---------- ----------- 
a          1
a          3
a          6
b          20
b          29
c          9
c          15
*/

解决方案 »

  1.   

    declare @t table(A varchar(10),B int)
    insert into @t select 'a',1
    insert into @t select 'a',2
    insert into @t select 'a',3
    insert into @t select 'b',20
    insert into @t select 'b',9
    insert into @t select 'c',9
    insert into @t select 'c',6
    declare @a table(id int identity(1,1),A varchar(10),B int)
    insert into @a select * from @tselect a.A,B=sum(b.B) from @a a,@a b where a.A=b.A and a.ID>=b.ID group by a.ID,a.A/*
    A          B           
    ---------- ----------- 
    a          1
    a          3
    a          6
    b          20
    b          29
    c          9
    c          15
    */
      

  2.   

    libin_ftsafe(子陌红尘) ( )大哥,俺崇拜你
      

  3.   

    五体投地,update居然玩到这种地步.....5555555........白活了.........