从多个父/子记录中获取Oracle SQL顶级父记录

时间:2016-12-13 15:57:58

标签: sql oracle11g hierarchical-data recursive-query connect-by

我试图创建一个Oracle SQL语句,以从不同级别的多个父级子记录中获取顶级根级父记录。表格结构如下。下面的顶级根父母是parent_membership_id 53887,这个父记录有许多孩子,他们也是其他孩子的父母。我想要的是一个查询,如果我查询说200326的成员,查询带回根成员53887,或者如果我查询200322我得到根成员53887.我想你知道我想要做什么。谢谢你。

 CREATE TABLE MEMBERSHIP_LINK
 ( MEMBERSHIP_LINK_ID        NUMBER(10)          NOT NULL,
   CHILD_MEMBERSHIP_ID       NUMBER(10)          NOT NULL,
   PARENT_MEMBERSHIP_ID      NUMBER(10)          NOT NULL);

Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (35, 53890, 53887);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24475, 200322, 53887);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24476, 200322, 53887);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (34, 53889, 53888);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (5941, 112177, 53889);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (33, 53888, 53890);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24477, 200323, 200322);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24478, 200323, 200322);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24479, 200325, 200323);
Insert into MEMBERSHIP_LINK
   (MEMBERSHIP_LINK_ID, CHILD_MEMBERSHIP_ID, PARENT_MEMBERSHIP_ID)
 Values
   (24480, 200326, 200323);
COMMIT;

2 个答案:

答案 0 :(得分:0)

解决了问题。 SQL在下面。

SELECT DISTINCT meli.parent_membership_id
       FROM   MEMBERSHIP_LINK meli
       WHERE  LEVEL = ( SELECT max(level)
             FROM    MEMBERSHIP_LINK meli_in
             START WITH meli_in.child_membership_id = :membership_id
             CONNECT BY meli_in.child_membership_id = PRIOR meli_in.parent_membership_id )
       START WITH meli.child_membership_id = :membership_id
       CONNECT BY meli.child_membership_id = prior meli.parent_membership_id

答案 1 :(得分:0)

这个有效,但由于记录24477和24478定义了相同的关系,它需要不同的运算符。

 SELECT DISTINCT ML.PARENT_MEMBERSHIP_ID
   FROM MEMBERSHIP_LINK ml
  WHERE CONNECT_BY_ISLEAF = 1  -- Limit to the "Root" element(s)
  START WITH ML.CHILD_MEMBERSHIP_ID = :decendent_id
CONNECT BY ML.CHILD_MEMBERSHIP_ID = prior ML.PARENT_MEMBERSHIP_ID;

PRIOR关键字放在CONNECT BY上的PARENT_MEMBERSHIP_ID子句中会导致树遍历到根,并使" root"节点离开。

如果您正在尝试寻找共享祖先,则需要采用不同的方法:

WITH ancestry AS
  ( SELECT DISTINCT CONNECT_BY_ROOT CHILD_MEMBERSHIP_ID child_id
         , ML.PARENT_MEMBERSHIP_ID ancestor_id
         , level generation_gap
         , CONNECT_BY_ISLEAF
      FROM MEMBERSHIP_LINK ml
     START WITH ML.CHILD_MEMBERSHIP_ID in (:Descendent_ID1,:Descendent_ID2)
   CONNECT BY ML.CHILD_MEMBERSHIP_ID = prior ML.PARENT_MEMBERSHIP_ID
  )
 SELECT ancestor_id
   FROM ancestry
  WHERE child_id = :Descendent_ID1
INTERSECT
 SELECT ancestor_id
   FROM ancestry
  WHERE child_id = :Descendent_ID2;

从这里你可以确定最新的(最年轻的)共享祖先,最早的共享祖先和共同的血统:

WITH ancestry AS (
   SELECT DISTINCT CONNECT_BY_ROOT CHILD_MEMBERSHIP_ID child_id
        , ML.PARENT_MEMBERSHIP_ID ancestor_id
        , level generation_gap
        , CONNECT_BY_ISLEAF
     FROM MEMBERSHIP_LINK ml
    START WITH ML.CHILD_MEMBERSHIP_ID in (:Descendent_ID1,:Descendent_ID2)
  CONNECT BY ML.CHILD_MEMBERSHIP_ID = prior ML.PARENT_MEMBERSHIP_ID
  ), common AS (
   SELECT ancestor_id
     FROM ancestry
    WHERE child_id = :Descendent_ID1
INTERSECT
   SELECT ancestor_id
     FROM ancestry
    WHERE child_id = :Descendent_ID2
  )
 SELECT MIN( a.ANCESTOR_ID ) keep( dense_rank FIRST ORDER BY a.GENERATION_GAP ) Youngest_Ancestor
      , LISTAGG(a.ANCESTOR_ID, '->') within group (order by a.GENERATION_GAP) common_lineage
      , MIN( a.ANCESTOR_ID ) keep( dense_rank FIRST ORDER BY a.GENERATION_GAP desc ) Oldest_Ancestor
   FROM ancestry a
   JOIN common c
     ON a.ancestor_id = c.ancestor_id
  WHERE a.child_id    = :Descendent_ID1;