PostgreSQL以递归方式聚合节点

时间:2017-02-02 09:02:13

标签: sql database postgresql nodes

我在postgres中的表格如下所示。将数组中的这些值解释为在有向图中连接的节点的ID。我想得到的是可能的路径列表(匹配每行的最后一个ID与其他行的第一个ID)

数据:

  foo
-------
 {1}
 {2,7}
 {3,4}
 {4,6}
 {5}
 {6,8}
 {7}
 {8}

预期结果:

{1}
{2,7}
{3,4,6,8}
{5}

我尝试使用递归查询和窗口函数,但它不能像我预期的那样工作。

1 个答案:

答案 0 :(得分:2)

你是否寻找这样的东西:

WITH RECURSIVE x AS (
        -- choose first level - without more connections 
        SELECT id, id AS full_id, 1 AS level
          FROM foo
         WHERE NOT EXISTS (
               SELECT 1
                 FROM foo AS foo2
                WHERE foo.id != foo2.id
                  AND foo.id[1] = foo2.id[array_length(foo2.id, 1)])
        -- add tail
        UNION ALL
        SELECT x.id, x.full_id || foo.id[2:array_length(foo.id, 1)], level + 1
          FROM x
          JOIN foo ON (
               foo.id != x.id
               AND foo.id[1] = x.full_id[array_length(x.full_id, 1)]
               AND array_length(foo.id, 1) != 1)
), z AS ( 
   -- looks for maximum length
   SELECT max(level) OVER (PARTITION BY id), * FROM x
)
-- choose only with maximum length
SELECT full_id FROM z WHERE max = level