update 表
set 成本 = (a.数量 * a.单价 + a.库存量 * a.成本) / (a.数量 + a.库存量)
from 表 a
where a.单别 = '进货' 

解决方案 »

  1.   

    看错了哈 改一下
    update 表
    set 成本 = (a.数量 * a.单价 + a.库存量 * (select b.成本 from 表 b where a.产品 = b.产品 and b.单别 = '期初')) / (a.数量 + a.库存量)
    from 表 a
    where a.单别 = '进货'
      

  2.   

    drop table test--生成测试数据
    create table test(
    日期   datetime,
    单别   varchar(10),
    单号   varchar(10),
    产品   varchar(10),
    数量   int,
    单价   numeric(6,1),
    金额   numeric(6,1),
    库存量 int,
    成本   numeric(6,1))
    insert into test select null      ,'期初',null    ,'P001',null,null,null,100,9
    insert into test select null      ,'期初',null    ,'P002',null,null,null,40 ,5
    insert into test select null      ,'期初',null    ,'P003',null,null,null,90 ,20
    insert into test select '05/02/14','进货','050214','P001',100 ,10  ,1000,0  ,0
    insert into test select '05/02/14','进货','050214','P002',200 ,6   ,1200,0  ,0
    insert into test select '05/02/14','进货','050214','P003',10  ,22  ,220 ,0  ,0
    insert into test select '05/02/15','出货','050215','P001',200 ,9.5 ,1900,0  ,0
    insert into test select '05/02/15','出货','050215','P002',100 ,5.5 ,550 ,0  ,0
    insert into test select '05/02/16','进货','050216','P001',100 ,11  ,1100,0  ,0
    insert into test select '05/02/16','进货','050216','P002',200 ,6.5 ,1300,0  ,0
    insert into test select '05/02/16','进货','050216','P003',200 ,9.5 ,1900,0  ,0--更新库存量字段
    update a
    set
        a.库存量 = 
        (select 
             库存量 
         from 
             test 
         where 
             单别 = '期初' and 产品 = a.产品) 
        + 
        (select 
             sum(case 单别 when '进货' then 数量 when '出货' then -1*数量 end) 
         from test 
             where 单别 != '期初' and 产品 = a.产品 and 日期 <= a.日期)
    from
        test a
    where
        a.单别 != '期初'
    --借助游标更新成本字段
    declare @date datetime,@type varchar(10),@id varchar(10)
    declare @product varchar(10),@sum int,@price numeric(6,1)declare t_cursor cursor for 
    select 日期,单别,单号,产品,数量,单价 from test where 单别 != '期初' order by 日期,单号open t_cursorfetch next from t_cursor
    into @date,@type,@id,@product,@sum,@pricewhile @@fetch_status = 0
    begin
        if @type = '进货'
        begin
            if exists(select 1 from test where 单别 != '期初' and 产品 = @product and 单号<@id)
            begin
                update
                    test
                set 
                    成本 = (select 
                                ((库存量*成本)+(@sum*@price))/(库存量+@sum)
                            from 
                                test a
                            where 
                                a.单别 != '期初' 
                                and 
                                a.产品 = @product 
                                and 
                                a.单号 = (select max(单号) from test where 单别 != '期初' and 产品 = @product and 单号 < @id))
                where
                    单号 = @id and 产品 = @product
            end
            else
            begin
                update
                    test
                set
                    成本 = (select 
                                ((库存量*成本)+(@sum*@price))/(库存量+@sum)
                            from 
                                test a
                            where 
                                a.单别 = '期初' 
                                and 
                                a.产品 = @product)
                where
                    单号 = @id and 产品 = @product
            end
        end
        else
        begin
            if exists(select 1 from test where 单别 != '期初' and 产品 = @product and 单号<@id)
            begin
                update
                    test
                set 
                    成本 = (select 
                                成本
                            from 
                                test a
                            where 
                                a.单别 != '期初' 
                                and 
                                a.产品 = @product 
                                and 
                                a.单号 = (select max(单号) from test where 单别 != '期初' and 产品 = @product and 单号 < @id))
                where
                    单号 = @id and 产品 = @product
            end
            else
            begin
                update
                    test
                set
                    成本 = (select 成本 from test where 产品 = @product and 单别 = '期初')
                where
                    单号 = @id and 产品 = @product
            end
        end
        
        fetch next from t_cursor
        into @date,@type,@id,@product,@sum,@priceendclose t_cursor
    deallocate t_cursor
    --输出结果
    select * from test日期                     单别   单号    产品  数量  单价   金额    库存量  成本
    -----------------------  -----  ------  ----  ----  -----  ------  ------  -----
    NULL                     期初   NULL    P001  NULL  NULL   NULL    100     9.0
    NULL                     期初   NULL    P002  NULL  NULL   NULL    40      5.0
    NULL                     期初   NULL    P003  NULL  NULL   NULL    90      20.0
    2005-02-14 00:00:00.000  进货   050214  P001  100   10.0   1000.0  200     9.5
    2005-02-14 00:00:00.000  进货   050214  P002  200   6.0    1200.0  240     5.8
    2005-02-14 00:00:00.000  进货   050214  P003  10    22.0   220.0   100     20.2
    2005-02-15 00:00:00.000  出货   050215  P001  200   9.5    1900.0  0       9.5
    2005-02-15 00:00:00.000  出货   050215  P002  100   5.5    550.0   140     5.8
    2005-02-16 00:00:00.000  进货   050216  P001  100   11.0   1100.0  100     11.0
    2005-02-16 00:00:00.000  进货   050216  P002  200   6.5    1300.0  340     6.2
    2005-02-16 00:00:00.000  进货   050216  P003  200   9.5    1900.0  300     13.1
      

  3.   

    用cursor处理10w笔左右的记录不知道速度怎样