同一查询中的多个递归引用

multiple recursive reference in same query

我知道 Postgres 不支持同一查询中的多个递归引用。所以我尝试重新编写查询以获得相同的结果,但到目前为止没有任何运气。

有人可以帮我实现这个目标吗?
Postgres 查询:-(在没有 'recursive' 的 Db2 中工作正常)

 with recursive parent(level,pid,name,depth) 
    as ( select t1.level,t1.pid,t1.name,0 from table1 t1, table2 t2,table3 t3 where t1.pid=t2.pid and t2.name=t3.name and t3.owner='me' 
    union all 
    select t11.level,t11.pid,t11.name,p.depth-1 from table1 t11, parent p where t11.pid = p.lev and p.depth<=0 
    union all 
    select t12.level,t12.pid,t12.name,p.depth+1 from table1 t12, parent p where t12.pid = p.pid and p.depth>=0)
select * from parent fetch first 5 rows only

**Error: recursive reference to query "parent" must not appear within its non-recursive term**

表 1
- - - - - - - - - - -
水平 |进程号 |姓名
.....................
11 | 1 | X1
3 | 2 | X2
1 | 4 | X4
6 | 11 | X11
4 | 12 | X12

表 2
- - - - - - - - - - -
进程号 |服务器 |名字
................... ...
12 | s1 | X12
4 | s11 | X4
1 | s1 | X1
2 |秒 | X2

表 3
- - - - - - -
拥有者 |姓名
...............
我 | X1
1 | X4
我 | X2

预期结果

列夫 |编号 |姓名 |深度
................... ...
1 | 4 | X4 | 0
11 | 1 | X1 | -1
6 | 11 | X11 | -2
1 | 4 | X4 | 2
1 | 4 | X4 | 3

试试这个

with recursive parent(level,pid,name,depth) 
    as (
         with recursive innerparent(level,pid,name,depth) 
           as (   select t1.level,t1.pid,t1.name,0 from table1 t1, table2 t2,table3 t3 
                    where t1.pid=t2.pid and t2.name=t3.name and t3.owner='me' 
               union all 
                  select t11.level,t11.pid,t11.name,p.depth-1 from table1 t11, 
                    innerparent p where t11.pid = p.lev and p.depth<=0 ) select * from 
                    innerparent
               union all 
                  select t12.level,t12.pid,t12.name,p.depth+1 from table1 t12, parent p 
                    where  t12.pid = p.pid and p.depth>=0)
select * from parent fetch first 5 rows only