Mrp计算物料净需求简例

--------------------------------------------------------------------------
--  Author : htl258(Tony)
--  Date   : 2010-04-24 21:00:07
--  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
--          Jul  9 2008 14:43:34
--          Copyright (c) 1988-2008 Microsoft Corporation
--          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--  Blog   : http://blog.csdn.net/htl258

--  Subject:Mrp计算物料净需求简例
--------------------------------------------------------------------------

--bom表(父类代码,子类代码,标准用量)
declare @bom table(
parentid
varchar(20),
childid
varchar(20),
funitqty
float
)
insert @bom select '8112212','p8112000',1
union all select 'p8112000','0752913ni',2
union all select '0752913ni','0752913t',1
union all select '4212202','0752913ni',2
union all select '0752913ni','0752913t',1

--订单数量表(物料代码,订单数量)
declare @mxqty table(
fnumber
varchar(20),
fmxqty
float)
insert @mxqty select '8112212',264
union all select '4212202',36

--可用库存表(物料代码,即时库存,预计入库量,已分配量)
declare @stock table(
fnumber
varchar(20),
fstockqty
float,
fexpectinqty
float,
fassignedqty
float
)
insert @stock
select '0752913ni',10,100,50  --60(即时库存+ 预计入库量- 已分配量)
union all
select '0752913t',25,500,100 --425(即时库存+ 预计入库量- 已分配量)

--计算公式:净需求量= 毛需求- (即时库存+ 预计入库量- 已分配量)
--
0752913ni:    540  = 600    -  60 
--
0752913t :    775  = 1200   -  425
--
p8112000 :    264  = 264    -  0
--
select * from @bom
--
select * from @mxqty
--
select * from @stock
;with t as
(
   
select a.*,planqty=funitqty*b.fmxqty ,lvl=0
   
from  @bom a
       
join (select fnumber ,sum(fmxqty ) fmxqty @mxqty

             from 

             group by fnumber )  b
           
on a.parentid=b.fnumber
   
union all
   
select a.*,a.funitqty*b.planqty,lvl+1
   
from @bom a
       
join t b
           
on b.childid=a.parentid
)
,t1
as
(
   
select childid,sum(planqty) planqty
   
from t a
   
group by childid
)
select fnumber=a.childid,
    mustqty
=a.planqty-(isnull(b.fstockqty,0)
           
+isnull(b.fexpectinqty,0)-isnull(b.fassignedqty,0))
from t1 a
   
left join @stock b
       
on a.childid=b.fnumber
       
/*--result:

fnumber              mustqty
-------------------- ----------------------
0752913ni            540
0752913t             775
p8112000             264

(3 行受影响)
*/


问题贴:http://topic.csdn.net/u/20100305/14/643ed33f-a5b1-4806-a009-1ae03b1ce0d3.html?seed=690801239&r=64948015#r_64948015

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值