SQL计算物料需求最佳效率
从SQL7一直用到SQL 2008,一直以来形成的思想,总觉得SQL不如ORACLE。
最近没事研究一下几个大型ERP的关键算法。 大家有兴趣不妨试试。
也解决我多年来的困扰,在此先谢过。有分送。
A(1PCS)
__|__
| |
B(2PCS) C(3G)
__|__
| |
C(1G) D(2G)
已知2009-09-26客户需要A(2000PCS),A是成品,B是自制品,C、D是原材料。
订单类型日期 料号 数量 状态
销售单2009-09-27 A 2000PCS 已下单
仓库2009-08-10 B 1000PCS 现有库存
如何展开此BOM?如何计算MRP。
create table bom_a(
parentno varchar(20),
qtynumeric(5,2),
unit char(5)
)
INSERT bom_a SELECT 'A ',1.0, 'PCS '
UNION ALL SELECT 'B ',1.0, 'PCS '
create table bom_b(
parentno varchar(20),
childno varchar(20),
qtynumeric(5,2),
unit char(5)
)
INSERT bom_b SELECT 'A ', 'B ',2.0, 'PCS '
UNION ALL SELECT 'A ', 'C ',3.0, 'G '
UNION ALL SELECT 'B ', 'C ',1.0, 'G '
UNION ALL SELECT 'B ', 'D ',2.0, 'G '
select * from bom_a
select * from bom_b
/*
parentno qty unit
-------------------- --------------------------------------- -----
A 1.00 PCS
B 1.00 PCS
(2 行受影响)
parentno childno qty unit
-------------------- -------------------- --------------------------------------- -----
A B 2.00 PCS
A C 3.00 G
B C 1.00 G
B D 2.00 G
(4 行受影响)
*/
drop table bom_a
drop table bom_b
[解决办法]
--参考CREATE TABLE BOM(PID INT,ID INT)INSERT INTO BOM SELECT 801,101INSERT INTO BOM SELECT 801,102INSERT INTO BOM SELECT 801,103INSERT INTO BOM SELECT 801,601INSERT INTO BOM SELECT 601,101INSERT INTO BOM SELECT 601,105INSERT INTO BOM SELECT 601,501INSERT INTO BOM SELECT 501,106INSERT INTO BOM SELECT 501,121GOCREATE FUNCTION F_GETROOT(@PID INT)RETURNS INTASBEGIN DECLARE @ID INT WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID) BEGIN SET @ID=@PID SELECT @PID=PID FROM BOM WHERE ID=@ID END RETURN @PIDENDGOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOMGO/*PID ID----------- ----------- 801 101801 102801 103801 601801 101801 105801 501801 106801 121*/DROP FUNCTION F_GETROOTDROP TABLE BOMGO--生成测试数据create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute varchar(20))insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'insert into BOM_1 select 3 ,'A2','A3',3,'生产'insert into BOM_1 select 4 ,'A2','A4',2,'采购'insert into BOM_1 select 5 ,'A3','A5',2,'采购'insert into BOM_1 select 6 ,'A3','A6',1,'采购'insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'insert into BOM_1 select 9 ,'B2','B3',3,'生产'insert into BOM_1 select 10,'B2','B4',2,'采购'insert into BOM_1 select 11,'B3','B5',2,'采购'insert into BOM_1 select 12,'B3','B6',2,'采购'go --创建用户定义函数,用于取每个父节点下子节点的采购配置信息create function f_stock(@bom_head varchar(20))returns @t table(bom varchar(20),number int)asbegin declare @level int declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int) set @level=1 if exists(select 1 from BOM_1 where bom_head=@bom_head) insert into @a select bom_child,number,products_attribute,@level from BOM_1 where bom_head=@bom_head while exists(select 1 from @a where [level]=@level and products_attribute='生产') begin set @level=@level+1 insert into @a(bom,number,products_attribute,[level]) select a.bom_child,a.number,a.products_attribute,@level from BOM_1 a,@a b where a.bom_head=b.bom and b.[level]=@level-1 end insert into @t(bom,number) select bom,number from @a where products_attribute='采购' returnendgo--执行调用,取父节点'A'一个标准配置分解的采购信息及数量select * from dbo.f_stock('A')--生成测试数据create table BOM(ID INT,PID INT,MSG VARCHAR(1000))insert into BOM select 1,0,NULLinsert into BOM select 2,1,NULLinsert into BOM select 3,1,NULLinsert into BOM select 4,2,NULLinsert into BOM select 5,3,NULLinsert into BOM select 6,5,NULLinsert into BOM select 7,6,NULLgo--创建用户定义函数用于取每个父节点下子节点的采购配置信息create function f_getChild(@ID VARCHAR(10))returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)asbegin declare @i int set @i = 1 insert into @t select ID,PID,@i from BOM where PID = @ID while @@rowcount<>0 begin set @i = @i + 1 insert into @t select a.ID,a.PID,@i from BOM a,@t b where a.PID=b.ID and b.Level = @i-1 end returnendgo--执行查询select ID from dbo.f_getChild(3)go--输出结果/*ID----567*/--删除测试数据drop function f_getChilddrop table BOM创建用户定义函数,每个子节点de父节点的信息--生成测试数据create table BOM(ID int,parentID int,sClassName varchar(10))insert into BOM values(1,0,'1111' )insert into BOM values(2,1,'1111_1' )insert into BOM values(3,2,'1111-1-1' )insert into BOM values(4,3,'1111-1-1-1') insert into BOM values(5,1,'1111-2' )go--创建用户定义函数,每个子节点de父节点的信息create function f_getParent(@ID int)returns varchar(40)asbegin declare @ret varchar(40) while exists(select 1 from BOM where ID=@ID and parentID<>0) begin select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'') from BOM a,BOM b where a.ID=@ID and b.ID=a.parentID end set @ret=stuff(@ret,1,1,'') return @retendgo--执行查询select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOMgo--输出结果/*ID parentID ----------- ---------------------------------------- 1 2 13 1,24 1,2,35 1 */--删除测试数据drop function f_getParentdrop table BOMgo
[解决办法]
----------------------------------- Author: htl258(Tony)-- Date : 2009-08-13 08:57:14-----------------------------------> 生成测试数据表:销售订单If not object_id('[销售订单]') is null Drop table [销售订单]GoCreate table [销售订单]([日期] Datetime,[料号] nvarchar(1),[数量] decimal(18,2),[状态] nvarchar(3))Insert 销售订单Select '2009-09-27','A','2000','已下单'Go--> 生成测试数据表:存货表If not object_id('[存货表]') is null Drop table [存货表]GoCreate table [存货表]([日期] Datetime,[料号] nvarchar(1),[数量] decimal(18,2),[状态] nvarchar(4))Insert 存货表Select '2009-08-10','B','1000','现有库存'Go--> 生成测试数据表:bom_aIf not object_id('[bom_a]') is null Drop table [bom_a]GoCreate table [bom_a]([parentno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))Insert bom_aSelect 'A',1.0,'pcs' union allSelect 'B',1.0,'pcs'Go--Select * from bom_a--> 生成测试数据表:bom_bIf not object_id('[bom_b]') is null Drop table [bom_b]GoCreate table [bom_b]([parentno] nvarchar(1),[childno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))Insert bom_bSelect 'A','B',2.0,'pcs' union allSelect 'A','C',3.0,'G' union allSelect 'B','C',1.0,'G' union allSelect 'B','D',2.0,'G'Go--Select * from bom_b/* A(1PCS) __|__ | | B(2PCS) C(3G) __|__ | | C(1G) D(2G) */-->SQL查询如下:;with t as ( select *,qty as xs--,0 as lvl from ( select null as parentno,[parentno] as childno,[qty],[unit] from bom_a union all select * from bom_b ) as tb where childno='A' union all select a.*,b.qty--,b.lvl+1 from ( select null as parentno,[parentno] as childno,[qty],[unit] from bom_a union all select * from bom_b ) as a join t as b on a.[parentno]=b.[childno]), t1 as( select childno 料号,sum(qty*xs) as auxqty,unit from t group by childno,unit )select a.料号,需求数=cast(a.auxqty*b.数量-isnull(c.数量,0) as dec(18,2)),a.unit 单位 from t1 a join [销售订单] b on a.料号<>b.料号 left join [存货表] c on a.料号=c.料号/*料号 需求数 单位---- --------------------------------------- ----C 10000.00 GD 8000.00 GB 3000.00 pcs(3 行受影响)*/
[解决办法]
--> 生成测试数据表:bom_aIf not object_id('[bom_a]') is null Drop table [bom_a]GoCreate table [bom_a]([parentno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))Insert bom_aSelect 'A',1.0,'pcs' union allSelect 'B',1.0,'pcs'Go--Select * from bom_a--> 生成测试数据表:bom_bIf not object_id('[bom_b]') is null Drop table [bom_b]GoCreate table [bom_b]([parentno] nvarchar(1),[childno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))Insert bom_bSelect 'A','B',2.0,'pcs' union allSelect 'A','C',3.0,'G' union allSelect 'B','C',1.0,'G' union allSelect 'B','D',2.0,'G'Go-->SQL查询如下:;with t as ( select px=cast(childno as varchar(max)),*,0 as lvl from ( select null as parentno,[parentno] as childno,[qty],[unit] from bom_a union all select * from bom_b ) as tb where parentno is null union all select cast(b.px+'.'+a.childno as varchar(max)),a.*,b.lvl+1 from ( select null as parentno,[parentno] as childno,[qty],[unit] from bom_a union all select * from bom_b ) as a join t as b on a.[parentno]=b.[childno])select case when lvl=0 then childno else replicate(' ',lvl)+'|'+replicate('--',lvl)+childno end as ItemNo,qty,unit,lvlfrom t order by px,lvl/*ItemNo qty unit lvl-------- ---- ---- ----A 1.00 pcs 0 |--B 2.00 pcs 1 |----C 1.00 G 2 |----D 2.00 G 2 |--C 3.00 G 1B 1.00 pcs 0 |--C 1.00 G 1 |--D 2.00 G 1(8 行受影响)*/