--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (编码 varchar(3),仓库 varchar(2),库存 int,成本价 money)
insert into #T
select '001','02',2,1.2 union all
select '001','02',5,1.2 union all
select '001','02',6,1.2 union all
select '002','01',3,5.6 union all
select '002','02',2,5.6insert into #T
select '001','02',2,1.8 union all
select '002','02',3,6select *,最新成本价=sum(库存*成本价)over(partition by 编码)/sum(库存)over(partition by 编码) from #T
/*
编码 仓库 库存        成本价                最新成本价
---- ---- ----------- --------------------- ---------------------
001  02   2           1.20                  1.28
001  02   5           1.20                  1.28
001  02   6           1.20                  1.28
001  02   2           1.80                  1.28
002  02   3           6.00                  5.75
002  01   3           5.60                  5.75
002  02   2           5.60                  5.75
*/-->TRIGGER FOR INSERT
update #T set 成本价=(select sum(库存*成本价)/sum(库存) from #T where 编码=a.编码) from #T a
--where 编码 in (select 编码 from INSERTED)select * from #T order by 1
/*
编码 仓库 库存        成本价
---- ---- ----------- ---------------------
001  02   2           1.28
001  02   5           1.28
001  02   6           1.28
001  02   2           1.28
002  02   3           5.75
002  01   3           5.75
002  02   2           5.75
*/

解决方案 »

  1.   

    小楼用临时表是测试,你不直接用你的表就ok?
      

  2.   


    很受伤别人只是用临时表模拟测试数据而已。
      

  3.   


    俺替小楼受伤
      

  4.   

    编码   仓库    库存   成本价  
    001    02       2     1.2  
    001    03       5     1.2  
    002    01       3      5.6  
    002    02       2      5.6  成本价都是一样的,因为我是用移动加权,如果我本次进货如下:  注意:进货仓库是 02  编码     进货数量    单价  
    001         2       1.8  
    002         3       6  结果
    编码   仓库    库存   成本价 
    001    02      4      1.33
    001    03      5      1.33
    002    01      3      5.84
    002    02      5      5.84
    要把商品进到相应的仓库里面
      

  5.   

    我也想知道,正在找這方面的資料~~~~~
      

  6.   

    以后需再关注,现在先帮你顶一下