简体   繁体   中英

Parent and Child Recursion Logic in SQL Server

I need some help with the following code.

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

The output that I'm getting with this:

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

For any ParentPart that has level 2 or greater structure, I want to see the path's immediate parent

See below desired output for example

在此处输入图像描述

Really appreciate your help. Thanks!

Just don't build the path and instead store the component you start with

 ;WITH Relation AS (
  SELECT ParentPart, Component
  , 0 AS Level
  ,Component as start_component 
  FROM @BomStructure 
  UNION ALL
  SELECT i.ParentPart, i.Component
  , Level + 1
  ,RL.start_Component
  FROM @BomStructure i 
  INNER JOIN Relation RL ON RL.ParentPart = i.Component)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM