select no,val,
       val2=(select sum(val) from tb where id<=t.id)
from tb t

解决方案 »

  1.   

    --> liangCK小梁 于2008-08-06
    --> 生成测试数据: @T
    declare @T table (no int,val int)
    insert into @T
    select 1,19 union all
    select 2,37 union all
    select 3,43 union all
    select 4,98select no,val,
           val2=(select sum(val) from @t where no<=t.no)
    from @T t/*
    no          val         val2
    ----------- ----------- -----------
    1           19          19
    2           37          56
    3           43          99
    4           98          197(4 行受影响)
    */
      

  2.   

    val2所在行的值就是小于val1所在行的累加和 
    如上