select identity(int,1,1) id,* into # from 你的表select ((select sum(b) from # where id<=tem.id and 字段c=tem.字段c)-(select sum(a) from # where id<=tem.id and 字段c=tem.字段c))/(select sum(b) from # where id<=tem.id and 字段c=tem.字段c) 结果 from # temdrop table #

解决方案 »

  1.   

    select *, identity(int,1,1) as id into #t from yourtableselect a, b, (select (sum(cast(a as numeric(8,4)) - sum(cast(b as numeric(8,4))) / sum(cast(b as numeric(8,4)) from #t where id <= A.id and c = A.c) result from #t as A
      

  2.   

    select *, identity(int,1,1) as id into #t from yourtableselect a, b, c, (select (sum(cast(a as numeric(8,4)) - sum(cast(b as numeric(8,4))) / sum(cast(b as numeric(8,4)) from #t where id <= A.id and c = A.c) result from #t as A
      

  3.   

    问题:字段a                字段b          字段c
            1                   7               dd
            3                   8               dd
         ..................
            X                   Y               dd   
            2                   4               rr 
            5                   9               rr是这样,DD的记录不只是两条,只是当DD变化时才不对上面的记录求各除,重头开始,是这个意思吗?
      

  4.   

    CrazyFor(蚂蚁) 你说的完全正确,我现在正在写这个储存过程,希望大家能
    帮帮我,谢谢了!!
      

  5.   

    tj_dns(愉快的登山者)你给的答案不 是我要的结果,你的出的result是按字段c分组求和了
      

  6.   

    heiguangbao(大头) ,我从天给你的那个能用吗?
      

  7.   

    CrazyFor(蚂蚁),你现在有空吗??我现在急着用,你能给我在写一个吗??
    其实是这样的,字段a字段b都是数字,我现在要按字段c分组,然后逐个记录进行求和,
    即:(b-a)/b*100求出每个求和记录的百分率,即平常我们用到的西格玛求和公式
    总之,几句话说不清,你能按上面的给我写一个吗?
      

  8.   

    create table #a(
    a int,
    b int,
    c char(2))select * from #a
    insert #a select 1,7,'dd'
    insert #a select 3,8,'dd'
    insert #a select 2,4,'rr'
    insert #a select 5,9,'rr'select *, identity(int,1,1) as id into #t from #aselect a, b, c, (select (sum(cast(b as numeric(8,4))) - sum(cast(a as numeric(8,4)))) / sum(cast(b as numeric(8,4))) 
     from #t where id <= A.id and c = A.c) result 
    from #t as Aa           b           c    result                                   
    ----------- ----------- ---- ---------------------------------------- 
    1           7           dd   .857142
    3           8           dd   .733333
    2           4           rr   .500000
    5           9           rr   .461538
      

  9.   

    select * from a0620declare @i numeric(18,6),@j numeric(18,6),@lastC varchar(50)
    set @i=0
    set @j=0
    set @lastC=''drop table #temp
    select *,cast(0 as numeric(18,6)) D into #temp from a0620
    update #temp 
        set 

    @j=case when @lastC=c then @j+b else b end
    ,@i=case when @lastC=c then @i+a else a end
    ,d=((@j)-(@i))/(@j)
    ,@lastC=c
    select * from #temp
    ---------------------------------------
    a0620
    --------
    a,b,c
    1.000000 7.000000 aaa
    3.000000 8.000000 aaa
    2.000000 4.000000 bbb
    5.000000 9.000000 bbb#temp
    ----------------
    a,b,c,d
    1.000000 7.000000 aaa .857143
    3.000000 8.000000 aaa .733333
    2.000000 4.000000 bbb .500000
    5.000000 9.000000 bbb .461538
      

  10.   

    select identity(int,1,1) id,* into # from 你的表select (select (sum(b)-sum(a)+0.0)/sum(b) from # where id<=tem.id and 字段c=tem.字段c) 结果 from # temdrop table #
      

  11.   

    select identity(int,1,1) id,* into # from 你的表select (select cast((sum(b)-sum(a)+0.0)/sum(b) as numeric(10,2))  from # where id<=tem.id and 字段c=tem.字段c) 结果 from # temdrop table #
      

  12.   

    大力,我用你的试过,可是得到的还是同一个值, tj_dns(愉快的登山者) 我也用你
    的试过,得到的有些值正确,有些值就不对了!!
      

  13.   

    好,我就把我的数据贴出来是在存储过程中的
    CREATE PROCEDURE sp_rpt_Material_Overspend_Underspend1
    @enquiry int=0,
    @workgroup varchar(100)='',
    @stockcode varchar(9)='',
    @year varchar(6)='',
    @variance varchar(6)=''
    as declare @sql varchar(2000),@sqlwhere varchar(500),@sqlworkgroup varchar(300),@sqlstockcode varchar(300)
    declare @sqlyear varchar(300),@sqlvariance varchar(300)
    if @enquiry=0
    begin
    set @sqlwhere='and T2_USAGE.actual_usage=''0'' and T3_USER_FORCAST.demand>=''1'''
    end
    else if @enquiry=1
    begin
    set @sqlwhere='and T2_USAGE.actual_usage>=''1'' and T3_USER_FORCAST.demand=''0'''
    end
    else if @enquiry=2
    begin
    set @sqlwhere='and (case isnull(T3_USER_FORCAST.demand,0) when 0 then 0 else convert(numeric(3),(T3_USER_FORCAST.demand-T2_USAGE.actual_usage)/T3_USER_FORCAST.demand*100) end) >=''15'''
    end
    else if @enquiry=3
    begin
    set @sqlwhere='and (case isnull(T3_USER_FORCAST.demand,0) when 0 then 0 else convert(numeric(3),(T3_USER_FORCAST.demand-T2_USAGE.actual_usage)/T3_USER_FORCAST.demand*100) end) <=''15'''
    end
    else 
    begin
    set @sqlwhere=''
    end if @workgroup=''
    begin
    set @sqlworkgroup=''
    end
    else 
    begin 
    set @sqlworkgroup='and T3_USER_FORCAST.work_group like '+ '''%'+@workgroup+'%'''
    end

    if @stockcode=''
    begin
    set @sqlstockcode=''
    end
    else
    begin
    set @sqlstockcode='and T1_STOCK_MASTER.stock_code like '+'''%'+ @stockcode+'%'''
    end if @year=''
    begin
    set @sqlyear='and DATEDIFF(year,cast(T3_USER_FORCAST.period+''01'' as datetime),getdate())=''0'''
    end
    else
    begin
    set @sqlyear='and DATEDIFF(year,cast(T3_USER_FORCAST.period+''01'' as datetime),cast( ' +''''+@year+''''+'+''0101'' as datetime))=''0'''
    end if @variance=''
    begin
    set @sqlvariance=''
    end
    else
    begin
    set @sqlvariance='and (case isnull(T3_USER_FORCAST.demand,0) when 0 then 0 else convert(numeric(3),(T3_USER_FORCAST.demand-T2_USAGE.actual_usage)/T3_USER_FORCAST.demand*100) end) ='+''''+@variance+''''
    end

    set @sql='
    select distinct T1_STOCK_MASTER.class,
    T1_STOCK_MASTER.stock_code,
    T3_USER_FORCAST.work_group,
    T1_STOCK_MASTER.item_name,
    T1_STOCK_MASTER.abc,
    T1_STOCK_MASTER.criticality,
    T1_STOCK_MASTER.soh,
    T6_ORDERS.invent_price,
    T6_ORDERS.invent_value,
    T3_USER_FORCAST.period,
    T2_USAGE.actual_usage,
    T3_USER_FORCAST.demand,
    (case isnull(T3_USER_FORCAST.demand,0) when 0 then 0 else convert(numeric(3),(T3_USER_FORCAST.demand-T2_USAGE.actual_usage)/T3_USER_FORCAST.demand*100) end) AS Monthly_Variance,
    identity(int,1,1) as id,
                    T4_PROJECTION.projection
    into #TEMP1
    from T1_STOCK_MASTER 
    inner join T3_USER_FORCAST 
    on T1_STOCK_MASTER.stock_code=T3_USER_FORCAST.stock_code
    inner join T6_ORDERS 
    on T1_STOCK_MASTER.stock_code=T6_ORDERS.stock_code
    inner join T2_USAGE 
    on T1_STOCK_MASTER.stock_code=T2_USAGE.stock_code
    and T3_USER_FORCAST.period=T2_USAGE.period

    inner join T4_PROJECTION 
    on T1_STOCK_MASTER.stock_code=T4_PROJECTION.stock_code
    and T3_USER_FORCAST.period=T4_PROJECTION.period
    where T1_STOCK_MASTER.category=''r''
    and T1_STOCK_MASTER.stock_type=''1''
    and T1_STOCK_MASTER.criticality<>''e''


    '+@sqlwhere+@sqlyear+@sqlworkgroup+@sqlstockcode+@sqlvariance
    +
    '
    select *, (select (sum(cast(demand as numeric(8,4))) - sum(cast(actual_usage as numeric(8,4)))) / sum(cast(demand as numeric(8,4))) 
     from #TEMP1 where id <= A.id and work_group = A.work_group) result 
    from #TEMP1 as Adrop table #TEMP1'
    exec(@sql)
    print @sql
    GO
      

  14.   

    tj_dns(愉快的登山者) ,我采用了你的方法。