我使用的是Oracle 12c。
我有一个分层的Oracle表,其中我想使用父节点的短名称(即start with parent_node_is is null)来表示属于该父节点的所有子节点。
例如:表名:nodes_tab
NODE_ID SHORT_NAME PARENT_NODE_ID
---------- -------------- --------------
1 Parent Node-1 NULL
2 Child Node-2 1
3 Child Node-3 1
4 Child Node-4 2
5 Child Node-5 2
6 Child Node-6 4
7 Child Node-7 6我想要实现的是查询上面的nodes_tab中的所有node_ids,但分配属于父节点的short_name。
理想情况下,我只想对其余的node_ids重复相同的名称,从2到7,但是不确定Parent Node-1查询应该是什么。我查看了LAG,但它似乎没有起到作用。
我想要的结果是:
NODE_ID SHORT_NAME
---------- -------------
1 Parent Node-1
2 Parent Node-1
3 Parent Node-1
4 Parent Node-1
5 Parent Node-1
6 Parent Node-1
7 Parent Node-1发布于 2019-05-22 17:24:34
在层次结构上,您的数据如下所示:
SQL> with nodes_tab (node_id, short_name, parent_node_id) as
2 (select 1, 'Parent Node-1', null from dual union all
3 select 2, 'Child Node-2' , 1 from dual union all
4 select 3, 'Child Node-3' , 1 from dual union all
5 select 4, 'Child Node-4' , 2 from dual union all
6 select 5, 'Child Node-5' , 2 from dual union all
7 select 6, 'Child Node-6' , 4 from dual union all
8 select 7, 'Child Node-7' , 6 from dual
9 )
10 select node_id,
11 lpad(' ', 2 * level) || short_name as short_name,
12 parent_node_id,
13 connect_by_root short_name as root_node
14 from nodes_tab
15 start with parent_node_id is null
16 connect by prior node_id = parent_node_id;
NODE_ID SHORT_NAME PARENT_NODE_ID ROOT_NODE
---------- ------------------------- -------------- -------------
1 Parent Node-1 Parent Node-1
2 Child Node-2 1 Parent Node-1
4 Child Node-4 2 Parent Node-1
6 Child Node-6 4 Parent Node-1
7 Child Node-7 6 Parent Node-1
5 Child Node-5 2 Parent Node-1
3 Child Node-3 1 Parent Node-1
7 rows selected.
SQL>注意ROOT_NODE,它是使用CONNECT_BY_ROOT获取的-似乎您希望所有的SHORT_NAME都有这个值。
因此:如果我们去掉缩进并应用上面看到的内容,再加上适当的ORDER BY子句,最终结果是
SQL> with nodes_tab (node_id, short_name, parent_node_id) as
2 (select 1, 'Parent Node-1', null from dual union all
3 select 2, 'Child Node-2' , 1 from dual union all
4 select 3, 'Child Node-3' , 1 from dual union all
5 select 4, 'Child Node-4' , 2 from dual union all
6 select 5, 'Child Node-5' , 2 from dual union all
7 select 6, 'Child Node-6' , 4 from dual union all
8 select 7, 'Child Node-7' , 6 from dual
9 )
10 select node_id,
11 connect_by_root short_name as short_name
12 from nodes_tab
13 start with parent_node_id is null
14 connect by prior node_id = parent_node_id
15 order by node_id;
NODE_ID SHORT_NAME
---------- -------------------------
1 Parent Node-1
2 Parent Node-1
3 Parent Node-1
4 Parent Node-1
5 Parent Node-1
6 Parent Node-1
7 Parent Node-1
7 rows selected.
SQL>发布于 2019-05-22 19:13:34
Oracle 11GR2及更高版本支持递归CTE(它是标准SQL的一部分)。
所以这是可行的:
with cte (node_id, parent_id, short_name, lev) as (
select node_id, coalesce(parent_node_id, node_id), short_name, 1
from nodes_tab
union all
select cte.node_id, nt.parent_node_id, nt.short_name, lev + 1
from cte join
nodes_tab nt
on cte.parent_id = nt.node_id
)
select *
from (select cte.*, row_number() over (partition by node_id order by lev desc) as seqnum
from cte
) cte
where seqnum = 1;Here是一个db<>fiddle。
https://stackoverflow.com/questions/56253269
复制相似问题