【问题标题】:Parent and Child Recursion Logic in SQL ServerSQL Server 中的父子递归逻辑
【发布时间】:2020-07-23 14:29:32
【问题描述】:

我需要以下代码方面的帮助。

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

对于任何具有 2 级或更高结构的 ParentPart,我想查看路径的直接父级

请参阅下面的所需输出示例

非常感谢您的帮助。谢谢!

【问题讨论】:

    标签: loops sql-server-2008 recursion common-table-expression


    【解决方案1】:

    只是不要构建路径,而是存储您开始使用的组件

     ;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)
    

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2012-08-02
      相关资源
      最近更新 更多