Мне нужна помощь со следующим кодом.
DECLARE @BomStructure TABLE
(
ParentPart Varchar(50),
Component Varchar(50)
) -- Parent & Child
INSERT @BomStructure VALUES ('P01-1000W', 'P01-1000WX');
INSERT @BomStructure VALUES ('P01-1000W', 'PKG-INSERT1000' );
INSERT @BomStructure VALUES ('P01-1000W', 'PKG-HANG CARD-XL' );
INSERT @BomStructure VALUES ('P01-1000W', 'PKG-BAG10' );
INSERT @BomStructure VALUES ('P01-1000W', 'PKG-BAG16');
INSERT @BomStructure VALUES ('P01-1000W', 'BOX RSC-01');
INSERT @BomStructure VALUES ('P01-1000W', 'F92-0306');
INSERT @BomStructure VALUES ('P01-1000WX', 'P01-1000W-TOP');
INSERT @BomStructure VALUES ('P01-1000WX', 'P01-1000W-BACK');
INSERT @BomStructure VALUES ('P01-1000WX', 'P01-1000W-BOTTOM');
INSERT @BomStructure VALUES ('P01-1000W-TOP', 'P12-5060WHA96');
INSERT @BomStructure VALUES ('P01-1000W-BACK', 'P12-5060WHA96');
INSERT @BomStructure VALUES ('P01-1000W-BOTTOM', 'P12-5060WHA96');
INSERT @BomStructure VALUES('PKG-INSERT1000', 'LABOR-KIT');
;WITH Relation AS
(
SELECT
ParentPart, Component,
0 AS Level,
CAST(Component AS VARCHAR(255)) AS Path
FROM
@BomStructure
UNION ALL
SELECT
i.ParentPart, i.Component,
Level + 1,
CAST(Path + '/' + CAST(i.Component AS VARCHAR(255)) AS VARCHAR(255)) AS Path
FROM
@BomStructure i
INNER JOIN
Relation RL ON RL.ParentPart = i.Component
)
SELECT *
FROM Relation
WHERE ParentPart = 'P01-1000W'
ORDER BY Level, Component
Вывод, который я получаю с этим:
ParentPart Component Level Path
--------------------------------------------
P01-1000W BOX RSC-01 0 BOX RSC-01
P01-1000W F92-0306 0 F92-0306
P01-1000W P01-1000WX 0 P01-1000WX
P01-1000W PKG-BAG10 0 PKG-BAG10
P01-1000W PKG-BAG16 0 PKG-BAG16
P01-1000W PKG-HANG CARD-XL 0 PKG-HANG CARD-XL
P01-1000W PKG-INSERT1000 0 PKG-INSERT1000
P01-1000W P01-1000WX 1 P01-1000W-BOTTOM/P01-1000WX
P01-1000W P01-1000WX 1 P01-1000W-BACK/P01-1000WX
P01-1000W P01-1000WX 1 P01-1000W-TOP/P01-1000WX
P01-1000W PKG-INSERT1000 1 LABOR-KIT/PKG-INSERT1000
P01-1000W P01-1000WX 2 P12-5060WHA96/P01-1000W-BOTTOM/P01-1000WX
P01-1000W P01-1000WX 2 P12-5060WHA96/P01-1000W-BACK/P01-1000WX
P01-1000W P01-1000WX 2 P12-5060WHA96/P01-1000W-TOP/P01-1000WX
Для любого ParentPart
, который имеет уровень 2 или Для большей структуры я хочу видеть непосредственного родителя пути
См. ниже требуемый вывод, например
Действительно признателен за вашу помощь. Спасибо!