求一先进先出的成本问题

hsmserver 2006-12-26 10:44:42
现有表结构如下:
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
...全文
271 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
st_2000 2006-12-28
  • 打赏
  • 举报
回复
我想知道先进先出法 更好的解决方案。。。。
生活真美好 2006-12-26
  • 打赏
  • 举报
回复
改一下库结构,用类型区分入出,是否更简单。
中国风 2006-12-26
  • 打赏
  • 举报
回复
看一下先
hsmserver 2006-12-26
  • 打赏
  • 举报
回复
是的,俺用游标实现,所以老感觉效率不是很好,期待更好的解决方案
playwarcraft 2006-12-26
  • 打赏
  • 举报
回复
SQL2000,function好像不能對table update...
其實,圖省力的方式,將table copy到臨時表,等提交後再寫回table,
或者乾脆add a new column, 用來暫時更新,提交後再update
以上僅供參考...
hsmserver 2006-12-26
  • 打赏
  • 举报
回复
感谢playwarcraft
已经比我的方法好多了,
显示#t2只是一个草稿状态,所以显示的时候并不想更新#t1的数据,等数据真正提交之后更改#t1的数据,所以我的想法是写个类似f_Name('a',25)的函数来显示数据,
然后通过p_Name('a',25)来提交数据后更改#t1.OutCount
playwarcraft 2006-12-26
  • 打赏
  • 举报
回复
--try,看看是不是你要的.以#t1的id來控制先進先出

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 select 'a',10,20,0
union all select 'a',15,15,0
union all select 'b',5,20,0
union all select 'b',20,28,0

insert into #t2(GoodsNo,Counts ,Price)
select 'a',25,0
union all
select 'b',10,0



update #t1
set #t1.OutCount= case when (select sum(counts) from #t1 a
where a.goodsno=#t1.goodsno
and a.id<=#t1.id)<=#t2.counts
then #t1.counts
else case when (select sum(counts) from #t1 b
where b.goodsno=#t1.goodsno
and b.id<#t1.id)>=#t2.counts
then 0
else #t2.counts-(select sum(counts) from #t1 c where c.goodsno=#t1.goodsno and c.id<#t1.id)
end
end
from #t1
inner join #t2
on #t1.GoodsNo=#t2.GoodsNo

update #t2
set #t2.price=(select sum(#t1.OutCount*#t1.price) from #t1 where #t1.goodsno=#t2.goodsno) /#t2.counts


/*result:*/

/*#t1*/
select * from #t1

ID GoodsNO Counts Price OutCount
----------- -------------------- ----------- --------------------- -----------
1 a 10 10.0000 10
2 a 10 20.0000 10
3 a 15 15.0000 5
4 b 5 20.0000 5
5 b 20 28.0000 5

/*#t2*/
select * from #t2

ID GoodsNo Counts Price
----------- -------------------- ----------- -----------
1 a 25 15
2 b 10 24
hsmserver 2006-12-26
  • 打赏
  • 举报
回复
显示数据
#t2
'a',25,15
我想要一个f_Name('a',25)类似的函数,
然后有一个存储过程可以回写OutCount,例如p_Name('a',25)之后
#t1为
'a',10,10,10
'a',10,20,10
'a',15,15,5
'b',5,20,0
'b',20,28,0
playwarcraft 2006-12-26
  • 打赏
  • 举报
回复
--請給出#t2的數據,供測試...
hsmserver 2006-12-26
  • 打赏
  • 举报
回复
注:不能用游标

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧