PostgreSQL with RECURSIVE 查询通过分区键获取有序的父子链

Mar*_*arc 5 postgresql recursive-query with-statement common-table-expression window-functions

我在 PostgreSQL 9.6.6 上编写 sql 脚本时遇到问题,该脚本通过使用步骤的父子 ID 对进程中的步骤进行排序,并且按进程 ID 进行分组/分区。我在这里找不到这个特殊情况,所以如果我错过了它,我深表歉意,并请您在评论中向我提供解决方案的链接。

案例:我有一张表,如下所示:

processID | stepID | parentID
     1         1          NULL
     1         3           5
     1         2           4
     1         4           3
     1         5           1
     2         1          NULL
     2         3           5
     2         2           4
     2         4           3
     2         5           1
Run Code Online (Sandbox Code Playgroud)

现在,我必须从每个 processID 的 ParentID 为 NULL 的步骤开始对步骤进行排序。

注意:我不能简单地订购 StepID 或 ParentID,因为我在整个过程中放入的新步骤会获得比过程中最后一步更高的步骤 ID(连续生成代理键)。

我必须为每个 processID 订购步骤,我将收到以下输出:

processID | stepID | parentID
     1         1          NULL
     1         5           1
     1         3           5
     1         4           3
     1         2           4
     2         1          NULL
     2         5           1
     2         3           5
     2         4           3
     2         2           4
Run Code Online (Sandbox Code Playgroud)

我尝试使用带有 RECURSIVE 的 CTE 函数来执行此操作:

    WITH RECURSIVE
starting (processID,stepID, parentID) AS
  (
    SELECT b.processID,b.stepID, b.parentID
    FROM process b
    WHERE b.parentID ISNULL
  ),
  descendants (processID,stepID, parentID) AS
  (
    SELECT b.processID,b.stepID, b.stepparentID
    FROM starting b
    UNION ALL
    SELECT b.processID,b.stepID, b.parentID
    FROM process b

    JOIN descendants AS c ON b.parentID = c.stepID
)
SELECT * FROM descendants
Run Code Online (Sandbox Code Playgroud)

结果不是我要寻找的。由于我们有数百个进程,我收到一个列表,其中第一条记录是不同的 processID,其parentID 为 NULL 值。

我想我必须再次在 processID 上递归整个脚本,但不知道如何。

感谢您的帮助!

kli*_*lin 4

您应该计算每个步骤的级别:

with recursive starting as (
    select processid, stepid, parentid, 0 as level
    from process
    where parentid is null
union all
    select p.processid, p.stepid, p.parentid, level+ 1
    from starting s
    join process p on s.stepid = p.parentid and s.processid = p.processid
)
select *
from starting
order by processid, level

 processid | stepid | parentid | level 
-----------+--------+----------+-------
         1 |      1 |          |     0
         1 |      5 |        1 |     1
         1 |      3 |        5 |     2
         1 |      4 |        3 |     3
         1 |      2 |        4 |     4
         2 |      1 |          |     0
         2 |      5 |        1 |     1
         2 |      3 |        5 |     2
         2 |      4 |        3 |     3
         2 |      2 |        4 |     4
(10 rows)   
Run Code Online (Sandbox Code Playgroud)

当然,如果不需要,可以跳过最终选择中的最后一列。