SQL 查询以检索同一 table 中父子关系中的数据。基本上它是一个层次结构 table

SQL query to retrieve data which is in Parent and child relation in same table. Basically its a hierarchy table

有人可以帮我查询以获取数据,该数据是同一父子关系 table.it 是一个层次结构 table

例如:Table

   Parent_ID | Parent_Name | Child_ID | Child_Name
     1       |    A        |   2      |    B
     2       |    B        |   3      |    C
     3       |    C        |   4      |    Cc
     3       |    C        |   5      |    Ccc
     4       |    Cc       |   6      |    C_C
     1       |    A        |   7      |    D
     7       |    D        |   8      |    Dd
     9       |    E        |   10     |    Ee
     10      |    Ee       |   11     |    Eee

以上table的数据关系是

            For A -> B is child
                     For  B -> C is child
                           For  C -> Cc and Ccc are Childs
                                     For Cc -> C_C is child

想要检索以下 Table 格式的数据:

    ParentId | Parent_Name | Child_Name | Child_Name | Child_Name | Child_Name
        1    |     A       |     B      |     C      |   Cc       |    Null
        1    |     A       |     B      |     C      |   Ccc      |    Null
        1    |     A       |     B      |     C      |   Cc       |    C_C
        1    |     A       |     D      |     Null   |   Null     |    Null
        1    |     A       |     D      |     Dd     |   Null     |    Null
        9    |     E       |     Ee     |     Null   |   Null     |    Null
        9    |     E       |     Ee     |     Eee    |   Null     |    Null

另一个例子

地理

     Parent_ID | Parent_Name | Child_ID | Child_Name
       1       |    Asia     |   2      |    India
       2       |    India    |   3      |    SouthIndia
       3       |  SouthIndia |   4      |    karnataka
       3       |  SouthIndia |   5      |    Kerela
       4       |  karnataka  |   6      |    Bengaluru
       1       |    Asia     |   7      |    Bangladesh
       7       |  Bangladesh |   8      |    Dhaka
       9       |  Srilanka   |   10     |    Colombo
       10      |  Colombo    |   11     |    Colombo district
       12      |North America|   13     |    Canada
       13      | Canada      |   14     |    Alberta
       13      | Canada      |   15     |    Ontario
       15      | Ontario     |   16     |    Toronto

期待如下

Parent_ID|Continent    | Country    | Zone       | State    | City

   1     |   Asia      |  India     | SouthIndia | karnataka| Bengaluru
   1     |   Asia      |  India     | SouthIndia | Kerela   |   
   1     |   Asia      | Bangladesh |            | Dhaka    |   
   1     |   Asia      | Srilanka   |            | Colombo  |Colombo district
  12     |NorthAmerica | Canada     |            | Alberta  |    
  12     |NorthAmerica | Canada     |            | Ontario  | Toronto   

提前致谢...

虽然我没有 return 您确切期望的结果,因为其中一些行应该在其他行中考虑(使用发布的数据),请考虑使用 self joins 所有链接 LEFT JOIN

此外,需要一个子查询 WHERE 子句来过滤掉可能是 Children 本身的原始 table 1 的 Parents。

SELECT t1.Parent_ID, t1.Parent_Name AS Parent, 
       t1.Child_Name AS Child1, t2.Child_Name AS Child2, 
       t3.Child_Name AS Child3, t4.Child_Name AS Child4    
FROM ParentChild t1 
LEFT JOIN ParentChild AS t2 ON t1.Child_Name = t2.Parent_Name
LEFT JOIN ParentChild AS t3 ON t2.Child_Name = t3.Parent_Name
LEFT JOIN ParentChild AS t4 ON t3.Child_Name = t4.Parent_Name

WHERE (SELECT TOP 1 sub.Parent_Name 
       FROM ParentChild sub 
       WHERE sub.Child_Name = t1.Parent_Name) IS NULL

输出

Parent_ID   Parent  Child1  Child2  Child3  Child4
1           A       B       C       Cc      C_C
1           A       B       C       Ccc     
1           A       D       Dd              
9           E       Ee      Eee