从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),
qty numeric(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),
qty numeric(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,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GO
CREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
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 @PID
END
GO
SELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO
/*
PID ID
----------- -----------
801 101
801 102
801 103
801 601
801 101
801 105
801 501
801 106
801 121
*/
DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO
--生成测试数据
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)
as
begin
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='采购'
return
end
go
--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')
--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go
--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
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
return
end
go
--执行查询
select ID from dbo.f_getChild(3)
go
--输出结果
/*
ID
----
5
6
7
*/
--删除测试数据
drop function f_getChild
drop 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)
as
begin
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 @ret
end
go
--执行查询
select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
go
--输出结果
/*
ID parentID
----------- ----------------------------------------
1
2 1
3 1,2
4 1,2,3
5 1
*/
--删除测试数据
drop function f_getParent
drop table BOM
go
---------------------------------
-- Author: htl258(Tony)
-- Date : 2009-08-13 08:57:14
---------------------------------
--> 生成测试数据表:销售订单
If not object_id('[销售订单]') is null
Drop table [销售订单]
Go
Create 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 [存货表]
Go
Create table [存货表]([日期] Datetime,[料号] nvarchar(1),[数量] decimal(18,2),[状态] nvarchar(4))
Insert 存货表
Select '2009-08-10','B','1000','现有库存'
Go
--> 生成测试数据表:bom_a
If not object_id('[bom_a]') is null
Drop table [bom_a]
Go
Create table [bom_a]([parentno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
Insert bom_a
Select 'A',1.0,'pcs' union all
Select 'B',1.0,'pcs'
Go
--Select * from bom_a
--> 生成测试数据表:bom_b
If not object_id('[bom_b]') is null
Drop table [bom_b]
Go
Create table [bom_b]([parentno] nvarchar(1),[childno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
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'
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 G
D 8000.00 G
B 3000.00 pcs
(3 行受影响)
*/
--> 生成测试数据表:bom_a
If not object_id('[bom_a]') is null
Drop table [bom_a]
Go
Create table [bom_a]([parentno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
Insert bom_a
Select 'A',1.0,'pcs' union all
Select 'B',1.0,'pcs'
Go
--Select * from bom_a
--> 生成测试数据表:bom_b
If not object_id('[bom_b]') is null
Drop table [bom_b]
Go
Create table [bom_b]([parentno] nvarchar(1),[childno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
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'
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)+childno end as ItemNo,qty,unit,lvl
from 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 1
B 1.00 pcs 0
|--C 1.00 G 1
|--D 2.00 G 1
(8 行受影响)
*/
--> 生成测试数据表:bom_a
If not object_id('[bom_a]') is null
Drop table [bom_a]
Go
Create table [bom_a]([parentno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
Insert bom_a
Select 'A',1000.0,'pcs' union all
Select 'B',10.0,'pcs'
Go
--Select * from bom_a
--> 生成测试数据表:bom_b
If not object_id('[bom_b]') is null
Drop table [bom_b]
Go
Create table [bom_b]([parentno] nvarchar(1),[childno] nvarchar(1),[qty] decimal(18,2),[unit] nvarchar(3))
Insert bom_b
Select 'A','B',2000.0,'pcs' union all
Select 'A','C',3000.0,'G' union all
Select 'B','C',10.0,'G' union all
Select 'B','D',20.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)+childno end as ItemNo,qty,unit,lvl
from t
order by px,lvl
/*
ItemNo qty unit lvl
A 1000.00 pcs 0
|--B 2000.00 pcs 1
|----C 10.00 G 2
|----D 20.00 G 2
|--C 3000.00 G 1
B 10.00 pcs 0
|--C 10.00 G 1
|--D 20.00 G 1
(8 行受影响)
*/
本文作者:ydtpan 来源: http://java.csdn.net
CIO之家 www.ciozj.com 微信公众号:imciow