表中数据如下:
1    3
2    8
3    6
如何得到一下结果
1    3
2    11
3    17知道需求吧。

解决方案 »

  1.   

    select id,num=(select sum(num) from tb where id<=t.id) 
    from tb t
      

  2.   

    SELECT A.col1,col2 = (select sum(col2) from tb B where B.col1<=A.col1) from tb A
      

  3.   

    create table tb(id int,num int)
    insert into tb select 1,3
    insert into tb select 2,8
    insert into tb select 3,6select id,num=(select sum(num) 
    from tb where id<=t.id) from tb tid num
    1 3
    2 11
    3 17
      

  4.   

    select id,num=(select sum(num) from tb where id<=t.id) from tb t
      

  5.   

    if object_id('Plus') is not null drop table Plus
    create table Plus
    (
      id int,
      PlusName int 
    )
    insert into Plus select 1,3
    insert into Plus select 2,8
    insert into Plus select 3,6select id,PlusName=(select sum(PlusName) from Plus where id<=P.id) from Plus P
      

  6.   


    declare @temp table 
    ( a int ,
      b  int 
    )
    insert into @temp select 1,3
          union all select 2,8
          union all select 3,6 select a, b=(select sum(b)   from @temp j where j.a<=c.a ) from @temp c