编程思路如下:
    只对A表a列中的数据进行编程。数据类型是数值型的,a列中的数据有大于等于零的,也有小于零的数据。
计算公式如下:
                大于等于零数的数值累计总和
  ————————————————————————————
  |小于零数的数值累计总和|+大于等于零数的数值累计总和上面的公式是按照每10个记录循环计算一下,计算的结果填充到新增列b当中。
也就是说,第一次计算时,统计第1行到第10行之内所有大于等于零数的数值累计总和,除以第1行到第10行间所有数据的绝对值累计总和,所得的结果保留两位插入到新增列b的第10行当中,那b列的前9行自动为null。
第二次计算时,统计第2行到第11行之内所有大于等于零数的数值累计总和,除以第2行到第11行间所有数据的绝对值累计总和,所得的结果保留两位插入到新增列b的第11行当中。
第三次计算时,统计第3行到第12行之内所有大于等于零数的数值累计总和,除以第3行到第12行间所有数据的绝对值累计总和,所得的结果保留两位插入到新增列b的第12行当中。

就这样一直循环计算到a列当中的最后一个记录结束。
这个编程的难点就是首先要对a列中所统计的数据正负进行判断,然后再进行归类统计计算。
这个我编不好,所以只好请哪位编程高手帮一下忙,谢先!

解决方案 »

  1.   

    ----测试环境:declare  @tab table(a decimal(10,2) ,b decimal(10,2))insert @tab(a)
    select 1 union all
    select 2 union all
    select -3.2 union all
    select 3 union all
    select -1 union all
    select -15 union all
    select 76 union all
    select 41 union all
    select -1.44 union all
    select 21 union all
    select 123 union all
    select -41 union all
    select 41 union all
    select 51.4 union all
    select -31 select id=identity(int,1,1),a,b into #t from @tab
    declare @n decimal(10,2),@m decimal(10,2)update #t
    set 
       @n=(select sum(a) from #t where id>g.id-10 and id<=g.id),
       @m=(select sum(abs(a)) from #t where id>g.id-10 and id<=g.id),
       b=@n/@m
    from #t g
    where id>=10select a,b from #t
    drop table #t
      

  2.   

    建立测试环境
    declare @tab table(a float , b float)
    declare @i int
    set @i=1
    insert @tab select 34,null
    insert @tab select 3,null
    insert @tab select -234,null
    insert @tab select 23,null
    insert @tab select -3,null
    insert @tab select 155,null
    insert @tab select -1,null
    insert @tab select 31,null
    insert @tab select 41,null
    insert @tab select -13,null
    insert @tab select 21,null
    insert @tab select 41,null
    insert @tab select-14,null
    insert @tab select 81,null
    insert @tab select -178,null
    insert @tab select 71,null
    insert @tab select 51,null
    insert @tab select -51,null
    insert @tab select 17,null
    insert @tab select 81,nullselect id = identity(int,1,1),a,b into #b from @tab
    select * from #b
    declare @sql nvarchar(1000)
    declare @count int
    select @count=count(1) from #b
    while @i<=@count-9
    begin
    set @sql='update  #b set b =(select round(sum(a.a)/sum(abs(a.a)),2) from  (select top 10 a from #b where id not in ( select top '
    +cast((@i-1)as nvarchar(10))+' id from #b)) a) where id = '+cast((@i+9) as nvarchar(10))
    print @sql 
    exec(@sql)
    set @i=@i+1
    end
    select * from #b
    drop table #b
      

  3.   

    --建表
    create table test2
    (
    id int,
    a dec(9,2)
    )--插入数据
    insert into test2(id,a)
    select 1,1
    union all select 2,-2
    union all select 3,3
    union all select 4,-4
    union all select 5,5
    union all select 6,-6
    union all select 7,7
    union all select 8,-8
    union all select 9,9
    union all select 10,-10
    union all select 11,11
    union all select 12,-12
    union all select 13,13
    union all select 14,-14
    union all select 15,15
    union all select 16,-16
    union all select 17,17
    union all select 18,-18
    union all select 19,19
    union all select 20,-20--查询select id ,a,
    case when id > 9 then (sum(case when a2 >= 0 then a2 else 0 end)/sum(case when a2 < 0 then -a2 else a2 end)) else null end as b 
    from 
    (
    select t1.id,t1.a,t2.id as id2,t2.a as a2 
    from test2 t1 left join test2 t2 
    on t2.id in (select top 10 id from test2 t3 where t3.id <= t1.id order by id desc)
    )aa
    group by id,a
    order by id
    --结果
    1 1.00 NULL
    2 -2.00 NULL
    3 3.00 NULL
    4 -4.00 NULL
    5 5.00 NULL
    6 -6.00 NULL
    7 7.00 NULL
    8 -8.00 NULL
    9 9.00 NULL
    10 -10.00 .454545
    11 11.00 .538461
    12 -12.00 .466666
    13 13.00 .529411
    14 -14.00 .473684
    15 15.00 .523809
    16 -16.00 .478260
    17 17.00 .520000
    18 -18.00 .481481
    19 19.00 .517241
    20 -20.00 .483870
      

  4.   

    create table test(id int identity(1,1),a float,b float)
    insert into test(a)
    select 1 union all
    select 1 union all
    select -1 union all
    select -1 union all
    select 2 union all
    select -2 union all
    select 1 union all
    select 3 union all
    select 3 union all
    select 1 union all
    select 1 union all
    select -1 union all
    select 2declare @i int
    set @i = 0
    declare @sql nvarchar(2000)
    while @i <= (select count(*) from test)
    begin
    if @i >= 10
    begin
    set @sql = 'update test set b = ((select sum(a) from test where a > 0 and id <= '+ ltrim(str(@i)) 
    +') - isnull((select sum(a) from test where a > 0 and id <= ' + ltrim(str(@i - 10)) 
    +'),0))/((select sum(abs(a)) from test where id <= ' + ltrim(str(@i)) 
    + ') - isnull((select sum(abs(a)) from test where id <= ' + ltrim(str(@i - 10))
    + '),0)) where id = ' + ltrim(str(@i))
    exec(@sql)
    end
    set @i = @i + 1  
    end
    select * from testdrop table test
      

  5.   

    试试这个:
    declare @tab table(id int identity(1,1),a float , b float)insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select-1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,null
    insert @tab select -1,null
    insert @tab select 1,null
    insert @tab select 1,nullselect t1.id,t1.a, b=cast(case when t1.id%10=0 then sum(case when t2.a >0 then t2.a else 0 end)/sum(abs(t2.a)) else null end as numeric(10,2))
    from @tab t1 join @tab t2 on (t1.id-1)/10=(t2.id-1)/10
    group by t1.id,t1.a
    order by t1.id--结果
      id     a        b
    --1 1.0 NULL
    --2 1.0 NULL
    --3 -1.0 NULL
    --4 1.0 NULL
    --5 -1.0 NULL
    --6 1.0 NULL
    --7 -1.0 NULL
    --8 1.0 NULL
    --9 1.0 NULL
    --10 -1.0 .60
    --11 1.0 NULL
    --12 1.0 NULL
    --13 -1.0 NULL
    --14 1.0 NULL
    --15 -1.0 NULL
    --16 1.0 NULL
    --17 -1.0 NULL
    --18 1.0 NULL
    --19 1.0 NULL
    --20 -1.0 .60
    --21 1.0 NULL
    --22 1.0 NULL
    --23 -1.0 NULL
    --24 1.0 NULL
    --25 -1.0 NULL
    --26 1.0 NULL
    --27 1.0 NULL
    --28 -1.0 NULL
    --29 1.0 NULL
    --30 1.0 .70