原表
aaa  11
bbb  22
ccc  33
累加后,第二条是前两条的求和,第三条是前三条的累加?
aaa  11
bbb  33
ccc  66

解决方案 »

  1.   

    declare @t table(a varchar(10),b int)
    insert into @t select 'aaa'  ,11
    union all select 'bbb'  ,22
    union all select 'ccc'  ,33select [id]=identity(int,1,1),* into # from @t
    select a,(select isnull(sum(b),0) from # where id<=a.id) as b from # adrop table #
      

  2.   

    declare @t table(a varchar(10),b int)
    insert into @t select 'aaa'  ,11
    union all select 'bbb'  ,22
    union all select 'ccc'  ,33select a,(select isnull(sum(b),0) from @t where a<=a.a) as b from @t a--如果a有规律这样也可以
      

  3.   

    declare @t table(a varchar(10),b int)
    insert into @t select 'aaa'  ,11
    union all select 'bbb'  ,22
    union all select 'ccc'  ,33
    select a,(select sum(b) from @t where a<=t.a) from @t t
    向楼上的学习
      

  4.   

    declare @t table ( a varchar(10),b int )
    insert into @t 
    select 'aaa',11 
    union all select 'bbb',22
    union all select 'ccc',33select id = identity(int,1,1) ,* into #temp from @t 
    select a, (select sum (b) from #temp where id <= a.id )
    from #temp a 
    drop table #temp
      

  5.   

    原表中存在有规则的列,则可以按2楼的写法,最好是标识列。
    如果没有,则一条语句可能不能完成。你要是说 exec('里面一大串')也是一条,那我没话说。
      

  6.   

    declare @t int
    set @t=0
    update [table]
    set @t=xx=xx+@t
      

  7.   

    我写了个笨笨的:
    create table aq(names varchar(22),num numeric(18,2))
    insert into aq(names,num)
    select 'aaa',11 union all
    select 'bbb',22 union all
    select 'ccc',33 union all
    select 'ddd',44SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS OFF 
    GO
    --exec   Tset
    ALTER        PROCEDURE Tset
    ASdeclare @nums numeric(18,2)
    declare @orders int
    declare @names varchar(20)
    declare @num intset @orders=0
    set @nums=0 select names,num,@orders as orders into #aq from aq where names='1'declare cur1 cursor for 
       select names,num from aqopen cur1
    fetch next from cur1 into @names,@numwhile @@fetch_status=0 
      Begin
         set @nums=@nums+isnull(@num,0)
         set @orders=@orders+1
         insert #aq(names,num,orders)
             Values(@names,@nums,@orders)
        
          
          fetch next from cur1 into @names,@num
    End
    close cur1
    DEALLOCATE cur1select names,num from #aq order by ordersset nocount off
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
      

  8.   

     回复人:bugchen888(臭虫) ( 一星(中级)) 信誉:100  2006-7-5 19:26:40  得分:0
    ?  declare @t int
    set @t=0
    update [table]
    set @t=xx=xx+@t
    ---------
    --cool