ton*_*nyf 1 sql oracle oracle12c
我正在使用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
Run Code Online (Sandbox Code Playgroud)
我想要实现的是在上面的nodes_tab中查询所有node_id,但是分配属于父节点的short_name。
理想情况下,我只想Parent Node-1对其余的node_id从2到7 重复相同的名称,但不确定SQL查询应该是什么。我看了看LAG,但似乎无法解决问题。
Result I am after is:
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
Run Code Online (Sandbox Code Playgroud)
Hierarchically, your data looks like this:
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>
Run Code Online (Sandbox Code Playgroud)
Note the ROOT_NODE, which is fetched by using CONNECT_BY_ROOT - it seems that you want that value for all SHORT_NAMEs.
So: if we remove indentation and apply what we've seen above, along with appropriate ORDER BY clause, the final result is
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |