簡體   English   中英

如何在connect by中獲得具有給定ID的完整子分支

[英]How to get full children branches with given ids in connect by

我想從parent_id = null轉到具有給定孩子ID的孩子

select *
from company s
start with s.parent_id is null and s.id = 56
connect by prior s.id = s.parent_id

結果是元素ID為56的完整分支

如何選擇與所選子代的分支

例:

現在的結果是:

id   parent_id
56     null
57      56
58      57
59      57
60      56
61      60 

我要實現的是給定的子ID:57僅選擇該分支

id   parent_id
56     null
57      56
58      57
59      57

without not given child id:
60      56
61      60 

這適用於樹的任何可能的深度,並且不使用正則表達式或字符串連接。 它應該很容易理解:

    with
       all_children_of_57 as 
       (     
          -- all nodes that can be reached by starting a recursive descent from node 57 
          select id
          from company s
          start with s.id = 57
          connect by prior s.id = s.parent_id 
       ),
       all_ancestors_of_57 as
       (
          -- all ANCESTORS that can be reached by walking up the tree (FROM CHILD TO PARENT), 
          -- starting from node 57 (this is a linear recursion)
          select id
          from company s
          start with s.id = 57  
          connect by  s.id = prior s.parent_id  
       ),
       nodes_in_branch as
       (  
          -- we are interested only in nodes extracted from the above two queries
          select * 
          from company
          where id in (select id from all_children_of_57)
             or id in (select id from all_ancestors_of_57)
       )

    -- we do the recursion on the result of nodes_in_branch  
    select *
    from nodes_in_branch s
    start with s.parent_id is null 
    connect by prior s.id = s.parent_id

一些簡單的查詢,應該滿足您的要求:

編輯:在查詢中添加注釋

EDIT2:最小化了SYS_CONNECT_BY_PATH的使用,因此更難以達到其大小限制。

    WITH param AS -- this is just our parameter to be used in further subqueries (ID and regex pattern for regexp_like expression)
(
  SELECT
    57 AS id
    ,'(\s?)('||57||')(\s|\W)' AS regex
  FROM
    dual
)
, parent AS -- here we get the inverted hierarchy - searching for the root node of the branch where ID given as parameter exists
(
SELECT
  s.*
FROM
  company s
  ,param
CONNECT BY PRIOR s.parent_id = s.id
START WITH s.id = param.id
)
, children AS -- here we get all the children of the root node we found in parent subquery
(
SELECT
  s.*
  ,CASE
    WHEN LEVEL >= MAX(CASE WHEN s.id = param.id THEN LEVEL ELSE NULL END) OVER (PARTITION BY NULL) THEN 
      SYS_CONNECT_BY_PATH(s.id,' ') 
    ELSE NULL
  END AS path -- this gives us the hierarchical path
  ,LEVEL AS lvl -- this gives us children's levels
  ,MAX(CASE WHEN s.id = param.id THEN LEVEL ELSE NULL END) OVER (PARTITION BY NULL) AS id_level -- this gives the level of the children given as a parameter
FROM
  company s
  ,param
CONNECT BY PRIOR s.id = s.parent_id
START WITH s.id = (SELECT parent.id FROM parent WHERE parent.parent_id IS NULL)
)
-- now we select from the all children hierarchy our desired branch
SELECT
  c.*
FROM
  children c
  ,param
WHERE
  1 = CASE WHEN lvl > c.id_level AND REGEXP_LIKE(c.path, param.regex) THEN 1 -- if current id is of higher level (is a child of our paremeter ID), it must have our parameter ID in it's path
           WHEN lvl = c.id_level AND c.id = param.id THEN 1 -- if the level is equal to our parameter's ID's leve, it has to be our paremeter
           WHEN lvl < c.id_level AND EXISTS(SELECT 1 FROM parent WHERE parent.id = c.id) THEN 1 -- if current level is lower (may be a parent of our parmeter ID) it has to exists in the reverse hierarchy (thus, must be a parent or grandparent etc. of our paremter node)
           ELSE 0
      END

順便說一句。 閱讀了Carlo Sirna的解決方案后,我發現他是一個更好的解決方案:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM