2

我想使用叶级别的条件进行分层查询。

我希望查询过滤叶级别满足条件的所有父子关系:id LIKE '3%'

表 t 是:

ParentId,Id
INSERT INTO t VALUES ('VTE', 'VTP');
INSERT INTO t VALUES ('VTP', '202');
INSERT INTO t VALUES ('SER', '606');
INSERT INTO t VALUES ('SER', '609');
INSERT INTO t VALUES ('GIF', '301');
INSERT INTO t VALUES ('ECH', '302');
INSERT INTO t VALUES ('PUB', 'MER');
INSERT INTO t VALUES ('MER', '312');
INSERT INTO t VALUES ('MER', '313');
INSERT INTO t VALUES ('MER', '314');
INSERT INTO t VALUES ('MES', '318');
INSERT INTO t VALUES ('PUB', 'PRE');
INSERT INTO t VALUES ('PUB', 'PAP');
INSERT INTO t VALUES ('STA', '317');
INSERT INTO t VALUES ('NIV', 'ANS');
INSERT INTO t VALUES ('ZNM', '497');
INSERT INTO t VALUES ('ZNU', '496');
INSERT INTO t VALUES ('ANS', 'ZNC');
INSERT INTO t VALUES ('ZNC', '491');
INSERT INTO t VALUES ('NUL', 'NIV');
INSERT INTO t VALUES ('NIV', 'VTE');
INSERT INTO t VALUES ('VTE', 'VTC');
INSERT INTO t VALUES ('VTC', '100');
INSERT INTO t VALUES ('VTP', '204');
INSERT INTO t VALUES ('VTP', '205');
INSERT INTO t VALUES ('VTA', '500');
INSERT INTO t VALUES ('SER', '600');
INSERT INTO t VALUES ('NIV', 'PUB');
INSERT INTO t VALUES ('ECH', '303');
INSERT INTO t VALUES ('MER', '305');
INSERT INTO t VALUES ('MER', '306');
INSERT INTO t VALUES ('MER', '309');
INSERT INTO t VALUES ('PAP', '605');
INSERT INTO t VALUES ('SEP', 'PBC');
INSERT INTO t VALUES ('PBC', '601');
INSERT INTO t VALUES ('SEP', 'STA');
INSERT INTO t VALUES ('NIV', 'TRA');
INSERT INTO t VALUES ('ZNP', '498');
INSERT INTO t VALUES ('ANS', 'ZNM');
INSERT INTO t VALUES ('ANS', 'ZNE');
INSERT INTO t VALUES ('ANS', 'ZNR');
INSERT INTO t VALUES ('ZNR', '493');
INSERT INTO t VALUES ('ZNF', '492');
INSERT INTO t VALUES ('VTC', '101');
INSERT INTO t VALUES ('VTC', '102');
INSERT INTO t VALUES ('VTE', 'VTA');
INSERT INTO t VALUES ('VTE', 'SER');
INSERT INTO t VALUES ('AUT', '900');
INSERT INTO t VALUES ('PUB', 'CPR');
INSERT INTO t VALUES ('MER', '310');
INSERT INTO t VALUES ('MER', '311');
INSERT INTO t VALUES ('MER', '604');
INSERT INTO t VALUES ('PUB', 'MES');
INSERT INTO t VALUES ('MES', '316');
INSERT INTO t VALUES ('SEP', 'RSF');
INSERT INTO t VALUES ('RSF', '608');
INSERT INTO t VALUES ('TRA', 'TRP');
INSERT INTO t VALUES ('TRP', '603');
INSERT INTO t VALUES ('ANS', 'ZNP');
INSERT INTO t VALUES ('ANS', 'ZNU');
INSERT INTO t VALUES ('ANS', 'ZNG');
INSERT INTO t VALUES ('ANS', 'ZNF');
INSERT INTO t VALUES ('VTC', '104');
INSERT INTO t VALUES ('VTC', '105');
INSERT INTO t VALUES ('VTP', '200');
INSERT INTO t VALUES ('VTP', '201');
INSERT INTO t VALUES ('VTP', '203');
INSERT INTO t VALUES ('VTA', '400');
INSERT INTO t VALUES ('VTE', 'AUT');
INSERT INTO t VALUES ('CPR', '602');
INSERT INTO t VALUES ('PUB', 'GIF');
INSERT INTO t VALUES ('PUB', 'ECH');
INSERT INTO t VALUES ('MER', '307');
INSERT INTO t VALUES ('MER', '308');
INSERT INTO t VALUES ('PRE', '304');
INSERT INTO t VALUES ('PRE', '315');
INSERT INTO t VALUES ('NIV', 'SEP');
INSERT INTO t VALUES ('TRP', '607');
INSERT INTO t VALUES ('ANS', 'ZNA');
INSERT INTO t VALUES ('ZNA', '499');
INSERT INTO t VALUES ('ZNG', '495');
INSERT INTO t VALUES ('ZNE', '494');
COMMIT;

我最初认为我可以使用类似的东西:

SELECT ParentId, Id
FROM t
WHERE  id LIKE '3%' 
start with ParentId ='NIV'
CONNECT BY PRIOR Id = ParentId

例如,302 的父亲是 ECH ECH 的父亲是 PUB PUB 的父亲是 NIV

但查询仅显示 0 级关系:302 的父亲是 ECH

它将所有更高级别丢弃到根:ECH的父亲是PUB PUB的父亲是NIV

我想出了下面的解决方案。

但是,子-父循环是硬编码的,这违背了分层查询的目的,即自动识别子-父关系的数量。

SELECT parentid, id
FROM t
WHERE id IN 
  (SELECT parentid
   FROM t
   WHERE 
   id IN 
     (SELECT parentid
      FROM t
      WHERE id LIKE '3%'
      start with parentid='NIV' 
      CONNECT BY PRIOR id = parentid)
      start with parentid ='NIV' 
      CONNECT BY PRIOR id = parentid)
OR 
ccomuse IN 
  (SELECT parentid 
   FROM t
   WHERE id LIKE '3%'
   start with parentid ='NIV' 
   CONNECT BY PRIOR id = parentid )
OR 
id LIKE '3%' 
start with parentid ='NIV' 
CONNECT BY PRIOR id = parentid 

有没有一种方法可以提取所有的子父关系,而无需对 2 个内部循环进行硬编码,即使用自动到达根的递归方法?

4

3 回答 3

0

尝试从错误的一端构建树:-)

SELECT distinct ParentId, Id
FROM t
start with Id LIKE '3%' 
CONNECT BY Id = prior ParentId and Id <> 'NIV'

小提琴

于 2013-03-24T18:05:45.833 回答
0

您是否查看过 sys_connect_by_path() 函数来逐项列出层次结构中的元素,并在最右侧进行过滤?

http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions171.htm#i1038266

于 2013-03-24T15:12:13.990 回答
0

如果要过滤叶级别满足条件的所有父子关系,则需要在 connect by 子句中指定条件。它将从输出中消除不必要的节点及其子节点。如果在 where 子句中指定条件,则子节点将显示没有父节点,因此输出将没有意义。

于 2017-08-06T18:49:06.237 回答