求一先进先出的成本问题
现有表结构如下:
CREATE TABLE #T1 --进货
(ID int IDENTITY(1,1),
GoodsNO varchar(20),--编号
Counts int,--数量
Price Money,--价格
OutCount int,--已出数量
)
CREATE TABLE #t2--出库
(
ID int IDENTITY(1,1),
GoodsNo varchar(20),--编号
Counts int,--数量
Price int,--成本
)
insert into #t1 (GoodsNo,Counts,Price,OutCount)
select 'a',10,10,0
union all 'a',10,20,0
union all 'a',15,15,0
union all 'b',5,20,0
union all 'b',20,28,0
根据#t2.Counts 求#t2.Price ,成本是指 不同进货批次的价格*数量的和/出库数量
并根据出库数量回写#t1.OutCount