简体   繁体   English

如何在 SQL 服务器中获取分层 CTE 到父子 MAX 日期

[英]How to get a hierarchical CTE in SQL Server to with parent and child MAX date

I have following table structure:我有以下表结构:

--===================================================================
-- Create and populate a table for demonstration purposes only:
--===================================================================

IF OBJECT_ID('tempdb..#BOM') IS NOT NULL DROP TABLE #BOM;
CREATE TABLE #BOM (
    ID INT,
    PartID int,
    ChildPartID int,
    FormulaId INT,
    FormulaDateTime datetime,
);

INSERT INTO #BOM
VALUES 
(   17372   ,   114798  ,   119892  ,   12636   ,'10/17/2021 13:40'),
(   17371   ,   118732  ,   119892  ,   12635   ,'10/17/2021 13:38'),
(   16623   ,   7900    ,   118732  ,   12071   ,'5/3/2021 8:32'),
(   16624   ,   7900    ,   115978  ,   12071   ,'5/3/2021 8:32'),
(   16625   ,   7900    ,   115757  ,   12071   ,'5/3/2021 8:32'),
(   16577   ,   118732  ,   114741  ,   12036   ,'4/24/2021 11:57'),
(   16348   ,   7900    ,   115765  ,   11869   ,'2/28/2021 11:47'),
(   16349   ,   7900    ,   114798  ,   11869   ,'2/28/2021 11:47'),
(   16350   ,   7900    ,   115757  ,   11869   ,'2/28/2021 11:47'),
(   15876   ,   115978  ,   115969  ,   11554   ,'11/2/2020 14:19'),
(   15763   ,   115765  ,   115463  ,   11465   ,'10/4/2020 16:10'),
(   15762   ,   115757  ,   115742  ,   11464   ,'10/4/2020 16:01'),
(   15222   ,   114798  ,   114741  ,   11047   ,'6/15/2020 16:52'),
(   13918   ,   7900    ,   9641    ,   9047    ,'8/28/2019 13:37'),
(   13919   ,   7900    ,   23568   ,   9047    ,'8/28/2019 13:37'),
(   13920   ,   7900    ,   16606   ,   9047    ,'8/28/2019 13:37'),
(   12005   ,   7900    ,   9635    ,   7370    ,'3/1/2019 15:58'),
(   12006   ,   7900    ,   9639    ,   7370    ,'3/1/2019 15:58'),
(   12007   ,   7900    ,   25609   ,   7370    ,'3/1/2019 15:58'),
(   11943   ,   25609   ,   11191   ,   7321    ,'2/19/2019 14:25'),
(   10019   ,   16606   ,   11029   ,   6645    ,'7/28/2018 13:07'),
(   9969    ,   23568   ,   11036   ,   6613    ,'7/21/2018 16:47'),
(   6222    ,   9635    ,   11036   ,   3070    ,'1/20/2018 15:45'),
(   6176    ,   9641    ,   11191   ,   3035    ,'1/14/2018 9:14'),
(   5712    ,   9639    ,   11029   ,   2701    ,'11/18/2017 10:28'),
(   6291    ,   7900    ,   9639    ,   1064    ,'10/2/2017 16:30'),
(   6292    ,   7900    ,   9635    ,   1064    ,'10/2/2017 16:30')

In this table, the product (PartID) is produced from one or more other products (ChildPartIDs) with the manufacturing formula number (formulaID) at the time (FormulaDateTime).在此表中,产品 (PartID) 是由一个或多个其他产品 (ChildPartID) 生产的,其制造公式编号 (formulaID) 在当时 (FormulaDateTime)。

    BOMID    PartId    ChildPartId  FormulaId   FormulaDateTime
     16623    7900      118732       12071       5/3/2021 8:32
     16624    7900      115978       12071       5/3/2021 8:32
     16625    7900      115757       12071       5/3/2021 8:32
     17371    118732    119892       12635       10/17/2021 13:38
     12005    7900      9635         7370        3/1/2019 15:58
       .
       .
       .

For example, a Part like with ID 7900, I want to get a hierarchical structure of formulas and constituent products(ChildPartID) based only on the last registered times of the formula.例如,像 ID 7900 这样的零件,我想仅根据公式的最后注册时间来获得公式和组成产品(ChildPartID)的层次结构。

  7900
   ├────118732
   │        └──119892
   ├────115978
   │        └──115969
   └────115757
            └──115742

According to this tree and table, to produce 7900 according to the time of the last formula, the formulaID is 12071. Therefore, at the first level, ChildPartIDs 118732, 115978 and 115757 are needed to produce 7900. For the next levels, it should be implemented in the same way.根据这棵树和表格,要根据上一个公式的时间产生 7900,formulaID 是 12071。因此,在第一级,需要 ChildPartID 118732、115978 和 115757 来产生 7900。对于下一级,它应该以同样的方式实现。 In fact, the condition to terminate is when there is no formula for the childPartID.事实上,终止的条件是当 childPartID 没有公式时。

Please help me how to implement stored procedure recursively to get this tree structure.请帮助我如何递归地实现存储过程来获得这个树结构。

Result, query should return: Result结果,查询应该返回: Result

This query does what you need此查询可以满足您的需要

WITH bom_rn AS (
    SELECT 
        *,
        RANK() OVER (PARTITION BY partid ORDER BY formuladatetime DESC) AS rn
    FROM #bom
),
product_tree AS (
    SELECT 
        *
    FROM bom_rn
    WHERE partid = 7900 AND rn = 1
    UNION ALL
    SELECT 
        bom_rn.*
    FROM bom_rn
    JOIN product_tree ON bom_rn.partid = product_tree.childpartid AND bom_rn.rn = 1
)
SELECT
    id,
    partid,
    childpartid,
    formulaid,
    formuladatetime
FROM product_tree

Query output查询output

id ID partid党派 childpartid孩子党 formulaid公式化 formuladatetime公式日期时间
16623 16623 7900 7900 118732 118732 12071 12071 2021-05-03 08:32:00.000 2021-05-03 08:32:00.000
16624 16624 7900 7900 115978 115978 12071 12071 2021-05-03 08:32:00.000 2021-05-03 08:32:00.000
16625 16625 7900 7900 115757 115757 12071 12071 2021-05-03 08:32:00.000 2021-05-03 08:32:00.000
15762 15762 115757 115757 115742 115742 11464 11464 2020-10-04 16:01:00.000 2020-10-04 16:01:00.000
15876 15876 115978 115978 115969 115969 11554 11554 2020-11-02 14:19:00.000 2020-11-02 14:19:00.000
17371 17371 118732 118732 119892 119892 12635 12635 2021-10-17 13:38:00.000 2021-10-17 13:38:00.000

You can check a working demo here您可以在此处查看工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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