层次关系SQL查询

Hierarchy relationship SQL query(层次关系SQL查询)
本文介绍了层次关系SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我提出以下问题?目标是转换实体,如预期输出所示.

Anyone can help me to come out a query for below ? The objective is to transform entity as shown in expected output.

Child   Parent  Name
0001    0001    HQ
0100    0001    HQ Accounting Dept
0200    0001    HQ Marketing Dept
0300    0001    HQ HR Dept
0101    0100    Branch North 111 
0102    0100    Branch North 112
0201    0200    Branch North 113
0301    0300    Branch North 114
8900    0300    Branch North 115
0387    8900    Sub Branch North 115

Expected output
----------------
Level1   Level2   Level3   Level4  Name
0001     0100     0101     N/A     Branch North 111
0001     0100     0102     N/A     Branch North 112
0001     0200     0201     N/A     Branch North 113
0001     0300     0301     N/A     Branch North 114
0001     0300     8900     0387    Sub Branch North 115

我试过查询,但答案并不正确

I've tried to query it but answer is not really correct

with cte as
(select Child,Parent from cmbc_entity
 where Parent = '0001'),
 cte2 as
 (select A.Parent as Level1, B.Child as Level2, C.Child as Level3, C.Name
 from cmbc_entity B inner join cte A on A.Child = B.Parent inner join cmbc_entity C on B.Child   =     C.Parent
 where B.Child != '0001')
 select * from cte2

推荐答案

以这种方式拉取数据集的局限性在于,您当然永远无法达到 4 个以上的级别.动态列是尝试和实施的主要痛苦.

The limitation with pulling the data set this way is that you will of course never be able to hit more than 4 levels. Dynamic columns is a major pain to try and implement.

您可能会设置一个递归 ID,但您的列更像是 Self、Parent、Grandparent、grandgrandparent、name,这会很快变得有点奇怪.

You could probably set up a recursive ID, but your column would be more like Self, Parent, Grandparent, grandgrandparent, name which could get a little weird quickly.

CREATE TABLE Department (
  DeptID INT,
  ParentID INT,
  Name VARCHAR(255));

INSERT INTO Department
VALUES
  (1,1,'HQ'),
  (100,1,'HQ Accounting Dept'),
  (200,1,'HQ Marketing Dept'),
  (300,1,'HQ HR Dept'),
  (101,100,'Branch North 111'),
  (102,100,'Branch North 112'),
  (201,200,'Branch North 113'),
  (301,300,'Branch North 114'),
  (8900,300,'Branch North 115'),
  (387,8900,'Sub Branch North 115');

;WITH cte_Level1 AS (
    SELECT
        CONVERT(VARCHAR(50), DeptID) [Level1],
        CONVERT(VARCHAR(50), 'N/A')  [Level2],
        CONVERT(VARCHAR(50), 'N/A')  [Level3],
        CONVERT(VARCHAR(50), 'N/A')  [Level4],
        Name
    FROM Department
    WHERE DeptID = ParentID
), cte_Level2 AS (
    SELECT * FROM cte_Level1 UNION ALL
    SELECT
        CONVERT(VARCHAR(50), A.Level1) [Level1],
        CONVERT(VARCHAR(50), D.DeptID) [Level2],
        CONVERT(VARCHAR(50), 'N/A')    [Level3],
        CONVERT(VARCHAR(50), 'N/A')    [Level4],
    D.Name
    FROM Department D
    INNER JOIN cte_Level1 A ON A.Level1 = D.ParentID
    WHERE D.DeptID <> D.ParentID
), cte_Level3 AS (
    SELECT * FROM cte_Level2 UNION ALL
    SELECT
        CONVERT(VARCHAR(50), A.Level1) [Level1],
        CONVERT(VARCHAR(50), A.Level2) [Level2],
        CONVERT(VARCHAR(50), D.DeptID) [Level3],
        CONVERT(VARCHAR(50), 'N/A')    [Level4],
        D.Name
    FROM Department D
    INNER JOIN cte_Level2 A ON A.Level2 = D.ParentID
        AND A.Level2 <> 'N/A'
    WHERE D.DeptID <> D.ParentID
), cte_Level4 AS (
    SELECT * FROM cte_Level3 UNION ALL
    SELECT
        CONVERT(VARCHAR(50), A.Level1) [Level1],
        CONVERT(VARCHAR(50), A.Level2) [Level2],
        CONVERT(VARCHAR(50), A.Level3) [Level3],
        CONVERT(VARCHAR(50), D.DeptID)    [Level4],
        D.Name
    FROM Department D
    INNER JOIN cte_Level3 A ON A.Level3 = D.ParentID
        AND A.Level3 <> 'N/A'
    WHERE D.DeptID <> D.ParentID
)

SELECT * FROM cte_Level4

这篇关于层次关系SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)