一张表中,数据已经有的数据如下,在执行一个累加后 变成下一张表,累加的规则是,从编号1开始,第一个不变,编号原来数据是8加上编号1的数据8 是等于16,然后在填回编号2,编号3的数据,是编号2+原来编号3的数据再填加回去
 编号             完成数              
  ==============================
  1                 8                
  2                 8                 
  3                 4                 
  4                 7                  
  5                 3                   
  6                 3       
    
  编号             完成数              
  =============================
  1                 8                
  2                 16                 
  3                 20                 
  4                 27                  
  5                 30                   
  6                 33      
谢谢帮忙

解决方案 »

  1.   


    select 编号,完成数=(select sum(完成数) from tb where 编号<=a.编号) from tb a
      

  2.   

    Select 编号,sum(完成数) from tb a where not exists(select 1 from tb where 编号<=a.编号)
      

  3.   

    select 
    编号,
    完成数=(select sum(完成数) from ta b where b.编号<=a.编号) 
    from ta a
      

  4.   

    SELECT 编号,(select sum(完成数) from LI where 编号 < A.编号)  AS 完成数 
    FROM LI A
      

  5.   

    declare @tb table(编号  int, 完成数  int)            insert @tb select 1,               8                
    insert @tb select 2 ,               8                
    insert @tb select 3 ,               4                
    insert @tb select 4 ,               7                  
    insert @tb select 5 ,               3                  
    insert @tb select 6 ,               3      declare @count int
    set @count=0
    update @tb set @count=@count+完成数,完成数=@count
    select * from @tb/*
    编号          完成数         
    ----------- ----------- 
    1           8
    2           16
    3           20
    4           27
    5           30
    6           33(所影响的行数为 6 行)
    */
      

  6.   

    select 编号,完成数=(select sum(完成数) from ta where 编号<=a.编号) from ta a
      

  7.   

    select t1.empid,t1.val ,SUM(t2.val)
    from 
    ashao as t1 
    join 
    ashao as t2
    on t1.empid = t2.empid
    and t1.val >= t2.val
    group by t1.empid,t1.val
    order by t1.empid