SQL (postgres) - child 的所有 parents 的递归视图

SQL (postgres) - Recursive view for all parents of a child

已解决以下:

我有两个表:community2collection(collection_id,community_id)community2community(child_comm_id,parent_comm_id)

我正在尝试创建一个递归视图,对于任何给定的集合 (collection_id) 将 return 直接社区列表 parent (community_id) ,以及直接社区的任何祖先(因此递归地通过 community2community.

我有

CREATE VIEW hierarchical_comcoltree
AS
    WITH RECURSIVE comcoltree AS
      (SELECT collection_id,
              community_id AS any_community_id
       FROM community2collection
       UNION ALL SELECT com2col.collection_id,
                        com2com.parent_comm_id AS any_community_id
       FROM community2collection com2col
       INNER JOIN community2community com2com
       ON com2com.child_comm_id = com2col.community_id)
    SELECT * FROM comcoltree;

但这只检索直接社区,以及它的第一个 parent,而不是进一步的祖先,查询

SELECT collection_id,
       any_community_id
FROM hierarchical_comcoltree
WHERE collection_id = '13081d45-dcdc-4e4d-94a5-33056c9f0c49'
ORDER BY collection_id ASC;

任何帮助将不胜感激,尝试了一些变体(以及一个函数),但还没有找到一个可行的...

测试数据

community2collection

collection_id community_id
13081d45-dcdc-4e4d-94a5-33056c9f0c49 17475c16-a03e-4f5f-8928-bef6a4813978
cbc98392-ba86-4ed3-b477-5a4a7652f3f0 88aa4abd-d024-47d2-a1a4-bdb3d744dd24

community2community

child_comm_id parent_comm_id
17475c16-a03e-4f5f-8928-bef6a4813978 ae12f497-b7de-4cb6-939f-70968a5e7b0a
ae12f497-b7de-4cb6-939f-70968a5e7b0a 05810e9b-cf09-4c13-a750-297cca5fd84f
88aa4abd-d024-47d2-a1a4-bdb3d744dd24 726a8531-4441-4feb-a8fc-b089445bc39e
726a8531-4441-4feb-a8fc-b089445bc39e ae12f497-b7de-4cb6-939f-70968a5e7b0a
ae12f497-b7de-4cb6-939f-70968a5e7b0a 05810e9b-cf09-4c13-a750-297cca5fd84f

=> '17475c16-a03e-4f5f-8928-bef6a4813978''ae12f497-b7de-4cb6-939f-70968a5e7b0a''05810e9b-cf09-4c13-a750-297cca5fd84f'

=> 缺少当前解决方案的最后一个

=> '88aa4abd-d024-47d2-a1a4-bdb3d744dd24''726a8531-4441-4feb-a8fc-b089445bc39e''ae12f497-b7de-4cb6-939f-70968a5e7b0a''05810e9b-cf09-4c13-a750-297cca5fd84f'

=> 缺少当前解决方案的最后两个

测试:

WITH RECURSIVE comcoltree AS
  (SELECT collection_id,
          community_id AS any_community_id
   FROM community2collection
   UNION ALL SELECT com2col.collection_id,
                    com2com.parent_comm_id AS any_community_id
   FROM community2collection com2col
   INNER JOIN community2community com2com ON com2com.child_comm_id = com2col.community_id)
SELECT collection_id,
       any_community_id
FROM comcoltree
WHERE any_community_id = 'cbc98392-ba86-4ed3-b477-5a4a7652f3f0'
ORDER BY collection_id ASC;

您的主要问题是您没有加入递归 CTE (comcoltree)。因此,您永远不会向带有 UNION 的结果集添加任何内容。这是我提供的最好的信息:

WITH RECURSIVE comcoltree AS
(
    SELECT collection_id
         , community_id     AS any_community_id
      FROM community2collection
     WHERE collection_id = 'cbc98392-ba86-4ed3-b477-5a4a7652f3f0'
     UNION ALL
    SELECT prev.collection_id
         , next.parent_comm_id AS any_community_id
      FROM comcoltree prev
      JOIN community2community next ON prev.any_community_id = next.child_comm_id
)
SELECT DISTINCT 
       collection_id
     , any_community_id
  FROM comcoltree
;

Here 是一个 DBFiddle 来展示它的工作原理。我把 distinct 放在那里是因为结果集中有重复项,但我认为这是因为样本数据中有重复项。

ae12f497-b7de-4cb6-939f-70968a5e7b0a 05810e9b-cf09-4c13-a750-297cca5fd84f