创建父级和子级层次结构,包括父级和子级的记录

628mspwn  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(314)

我有一个父子id表层次结构-例如。

|parent|child|
|------|-----|
|      |    0|
|     0|    1|
|     0|    2|
|     0|    3|
|     1|    4|
|     1|    5|
|     2|    6|
|     4|    7|
|     4|    8|

我正在构建一个可视化树层次结构,上面的数据将被格式化为:

|parent|child1|child2|child3
|------|------|------|------
|     0|     1|4     |     7
|     0|     1|4     |     8
|     0|     1|5     |
|     0|     2|6     |
|     0|     3|      |

现在我想修改这个查询,为每个父级包含一行,而不包含子级,因此上面的数据将变成:

|parent|child1|child2|child3
|------|------|------|------
|     0|      |      |
|     0|     1|      |
|     0|     1|     4|
|     0|     1|     4|     7
|     0|     1|     4|     8
|     0|     1|     5|
|     0|     2|      |
|     0|     2|     6|
|     0|     3|      |

为了得到第一个结果,我正在使用重复的左连接构建数据(使用上面的第一个数据示例),因为我的理解是,我不能用递归来实现这一点,例如:

SELECT t1.child AS parent
       t2.child AS child1
       t3.child AS child2
       t4.child AS child3
FROM id_table t1

LEFT JOIN id_table t2
ON t1.child = t2.parent

LEFT JOIN id_table t3
ON t1.child = t3.parent

LEFT JOIN id_table t4
ON t1.child = t4.parent

WHERE t1.child = '0'

这是第二个例子,但是我也缺少每个家长的记录,如第三个例子所示。
我想这可能是一个简单的问题,我只是在与语法斗争。谢谢你的帮助。
编辑:我之前有一个关于sas中类似实现的问题,例如:sql—每个级别都有记录的递归树层次结构,但是这是sas sql实现的问题,它的限制要大得多—使用该方法,我最终只能在每个级别创建临时表,然后合并最终结果,这很混乱。试图找到一个更干净的解决方案。

omvjsjqw

omvjsjqw1#

group by rollup可用于创建这些额外的行:

SELECT DISTINCT
   t1.child AS Parent
  ,t2.child AS child1
  ,t3.child AS child2
  ,t4.child AS child3
  -- one more column for each additional level
FROM id_table t1

LEFT JOIN id_table t2
ON t1.child = t2.Parent

LEFT JOIN id_table t3
ON t2.child = t3.Parent

LEFT JOIN id_table t4
ON t3.child = t4.Parent

-- one additional join for each new level
WHERE t1.child = '0'

GROUP BY ROLLUP (t1.child,t2.child,t3.child,t4.child) 
HAVING t1.child IS NOT NULL

或者使用递归查询遍历层次结构,构建路径,然后将其拆分为列:

WITH RECURSIVE cte AS
 ( -- traverse the hierarchy and built the path
   SELECT 1 AS lvl,
     ,child
     ,Cast(child AS VARCHAR(500)) AS Path -- must be large enough for concatenating all levels
   FROM id_table
   WHERE Parent IS NULL

   UNION ALL

   SELECT lvl+1
     ,t.child
     ,cte.Path || ',' || Trim(t.child)   
   FROM cte JOIN id_table AS t
   ON cte.child = t.Parent
   WHERE lvl < 20 -- just in case there's an endless loop
 ) 
SELECT 
   StrTok(Path, ',', 1) 
  ,StrTok(Path, ',', 2) 
  ,StrTok(Path, ',', 3) 
  ,StrTok(Path, ',', 4) 
  -- one additional StrTok for each new level
FROM cte

不知道哪一个更有效率。

相关问题