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
*/
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
*/
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
*/
insert into @t(A,B) select 'a',1
insert into @t(A,B) select 'a',2
insert into @t(A,B) select 'a',3
insert into @t(A,B) select 'b',20
insert into @t(A,B) select 'b',9
insert into @t(A,B) select 'c',9
insert into @t(A,B) select 'c',6declare @c int,@a varchar(10)
update @t
set c=@c,@c=(case when a=@a then @c+b else b end),@a=a
select * from @t