日期:2014-05-18  浏览次数:20529 次

bom結構表,卷積成本,求SQL
源材料表: T
T1品號 T1單價


工藝表: G
G1品號 G2工藝 G3單價 


BOM數據表
父項品號 序號 子項品號
A00 10 B00
B00 10 C00
A00 20 B01
A00 30 B02
B02 10 C01
C01 10 D11
C01 20 D22

BOM查詢表: B

B1低階碼 B2子項品號 B3子項序號 B5工藝 B6成本
.0 A00 10 G001 
...1 B00 10 G002
......2 C00 10  
...1 B01 20
...1 B02 30 G003  
......2 C01 10 G004  
.........3 D11 10  
.........3 D22 20  

以上BOM結構表經過一定的算法得出來的查詢結果,現在想從最底階碼向上卷積成本,沒有工藝的取材料成本,有工藝的取工藝成本
如:
  D11、D22成本加到C01上,C01雙加到B02上,B02、BO1、B00加A00上
不知這樣難度、耗用系統資源大不大,
  
 

------解决方案--------------------
BOM的一般处理,也就是这个样子, 在计算上,LZ可以参考一些算法(比如递归)。来提高查找的效率。 
另外,还有一个关键是要建好索引。 

------解决方案--------------------
SQL code

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