[英]connect-by-prior: using PRIOR to drill 2-levels up
I have the following connect-by-prior SQL which essentially starts at the leaf node and works its way up the tree to the parent "tree-trunc" (level-1):我有以下先验连接的 SQL,它基本上从叶节点开始,沿着树向上到达父“tree-trunc”(级别 1):
with my_tree as (
select 'level 4.1' node, 'level 3.1' parent_node from dual union
select 'level 4.2' node, 'level 3.2' parent_node from dual union
select 'level 3.1' node, 'level 2' parent_node from dual union
select 'level 3.2' node, 'level 2' parent_node from dual union
select 'level 2' node, 'level 1' parent_node from dual union
select 'level 1' node, '' parent_node from dual
)
select level, t.node, t.parent_node, prior t.node child_that_pointed_me_here
from my_tree t
connect by t.node = prior t.parent_node
start with t.node like 'level 4%'
It gives me the following output:它给了我以下 output:
level node parent_node child_who_pointed_me_here
----- --------- ----------- -------------------------
1 level 4.1 level 3.1
2 level 3.1 level 2 level 4.1
3 level 2 level 1 level 3.1
4 level 1 level 2
1 level 4.2 level 3.2
2 level 3.2 level 2 level 4.2
3 level 2 level 1 level 3.2
4 level 1 level 2
You can see that the instruction prior t.node
(aliased as column child_who_pointed_me_here
) takes me to the data in the "prior" record (ie the child-node from where I started), which is exactly what I want.您可以看到
prior t.node
的指令(别名为child_who_pointed_me_here
列)将我带到“之前”记录中的数据(即我开始的子节点),这正是我想要的。 In other words, the PRIOR
keyword gives me access to data in the "previous" record.换句话说,
PRIOR
关键字使我可以访问“上一个”记录中的数据。
But What I would like is to access data 2-levels (or 3 or 4 levels) previous.但我想要的是访问之前的 2 级(或 3 或 4 级)数据。 Something like
prior prior t.node
.类似于
prior prior t.node
。 The desired output would look as follows:所需的 output 如下所示:
level node parent_node child_who_pointed_me_here grandchild_who_pointed_me_here
----- --------- ----------- ------------------------- ------------------------------
1 level 4.1 level 3.1
2 level 3.1 level 2 level 4.1
3 level 2 level 1 level 3.1 level 4.1
4 level 1 level 2 level 3.1
1 level 4.2 level 3.2
2 level 3.2 level 2 level 4.2
3 level 2 level 1 level 3.2 level 4.2
4 level 1 level 2 level 3.2
I've tried the obvious prior prior t.node
, but it obviously just results in an unsupported-syntax type error.我已经尝试过明显的
prior prior t.node
,但它显然只会导致不支持的语法类型错误。
The question therefore: Is there a construct in connect-by SQL which would allow me to go 2 levels back (or up) along the path?因此,问题是: SQL 中是否有一个构造可以让我沿着路径返回(或向上)2 个级别?
I'm using Oracle 12c to construct this SQL, but answers in any flavour of SQL appreciated.我正在使用 Oracle 12c 来构建这个 SQL,但是任何风格的 SQL 的答案都值得赞赏。
the sys_connect_by_path will give you the entire lineage from where you have reached the current row. sys_connect_by_path 将为您提供到达当前行的整个沿袭。
I perform string manipulation on the sys_connect_by_path by reversing and choosing the index positions of the second '/' and third '/' to exract the data in between我通过反转和选择第二个“/”和第三个“/”的索引位置来对 sys_connect_by_path 执行字符串操作,以提取其间的数据
Using this string we can use the following to extract 2 layers up as follows.使用此字符串,我们可以使用以下内容提取 2 层,如下所示。
with my_tree as (
select 'level 4.1' node, 'level 3.1' parent_node from dual union
select 'level 4.2' node, 'level 3.2' parent_node from dual union
select 'level 3.1' node, 'level 2' parent_node from dual union
select 'level 3.2' node, 'level 2' parent_node from dual union
select 'level 2' node, 'level 1' parent_node from dual union
select 'level 1' node, '' parent_node from dual
)
select level, t.node, t.parent_node, prior t.node child_that_pointed_me_here
,sys_connect_by_path(t.node,'/') as lineage
,rtrim(
reverse(
substr(
reverse(sys_connect_by_path(t.node,'/'))
,instr(reverse(sys_connect_by_path(t.node,'/')),'/',1,2)
,instr(reverse(sys_connect_by_path(t.node,'/')),'/',1,3)
-
instr(reverse(sys_connect_by_path(t.node,'/')),'/',1,2)
)
)
,'/') as two_level_up
from my_tree t
connect by t.node = prior t.parent_node
start with t.node like 'level 4%'
+-------+-----------+-------------+----------------------------+--------------------------------------+--------------+
| LEVEL | NODE | PARENT_NODE | CHILD_THAT_POINTED_ME_HERE | LINEAGE | TWO_LEVEL_UP |
+-------+-----------+-------------+----------------------------+--------------------------------------+--------------+
| 1 | level 4.1 | level 3.1 | null | /level 4.1 | |
| 2 | level 3.1 | level 2 | level 4.1 | /level 4.1/level 3.1 | |
| 3 | level 2 | level 1 | level 3.1 | /level 4.1/level 3.1/level 2 | level 4.1 |
| 4 | level 1 | | level 2 | /level 4.1/level 3.1/level 2/level 1 | level 3.1 |
| 1 | level 4.2 | level 3.2 | | /level 4.2 | |
| 2 | level 3.2 | level 2 | level 4.2 | /level 4.2/level 3.2 | |
| 3 | level 2 | level 1 | level 3.2 | /level 4.2/level 3.2/level 2 | level 4.2 |
| 4 | level 1 | | level 2 | /level 4.2/level 3.2/level 2/level 1 | level 3.2 |
+-------+-----------+-------------+----------------------------+--------------------------------------+--------------+
dbfiddle https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=0448131cc387e52eab3126dfce0a7cde dbfiddle https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=0448131cc387e52eab3126dfce0a7cde
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.