oracle CONNECT_BY_ROOT 在组内搜索
oracle CONNECT_BY_ROOT searching within a group
给定如下table
KID | CROSS_LINK | KEY | GROUP_KEY
--------|------------|--------|-----------
COR001 | | 711768 | 336406
EXP001 | | 740194 | 336406
POD002 | DPR002 | 712408 | 336406
POD003 | POD002 | 718445 | 336406
DPR001 | | 711825 | 336406
TRC001 | | 713773 | 336406
DPR002 | | 712199 | 336406
POD001 | DPR001 | 712191 | 336406
POD004 | POD001 | 815174 | 336406
POD005 | POD003 | 815181 | 336406
POD002 | DPR001 | 478800 | 475873
POD005 | POD002 | 300266 | 475873
DPR001 | | 300202 | 475873
我的sql声明是
select level lvl, kid, CONNECT_BY_ROOT CROSS_LINK as father, key, group_key
from t
start with CROSS_LINK is not null
CONNECT BY PRIOR kid = CROSS_LINK
order by GROUP_KEY
您可以将 CROSS_LINK 视为父列。
我的目标是在 GROUP_KEY .
中显示 KID 的所有父亲(层次结构中的直接和间接)
此查询不以孤立的方式 运行 在 GROUP_KEY 中。您可以在 KID 'POD005' 的行为中观察到问题。在下图中,我突出显示了错误的行。
向您的 CONNECT BY
子句添加一个条件,以强制它只在与父行相同的 GROUP_KEY
中找到子项。
select level lvl, kid, CONNECT_BY_ROOT CROSS_LINK as father, key, group_key
from t
start with CROSS_LINK is not null
CONNECT BY PRIOR kid = CROSS_LINK
AND PRIOR group_key = group_key
order by GROUP_KEY
给定如下table
KID | CROSS_LINK | KEY | GROUP_KEY
--------|------------|--------|-----------
COR001 | | 711768 | 336406
EXP001 | | 740194 | 336406
POD002 | DPR002 | 712408 | 336406
POD003 | POD002 | 718445 | 336406
DPR001 | | 711825 | 336406
TRC001 | | 713773 | 336406
DPR002 | | 712199 | 336406
POD001 | DPR001 | 712191 | 336406
POD004 | POD001 | 815174 | 336406
POD005 | POD003 | 815181 | 336406
POD002 | DPR001 | 478800 | 475873
POD005 | POD002 | 300266 | 475873
DPR001 | | 300202 | 475873
我的sql声明是
select level lvl, kid, CONNECT_BY_ROOT CROSS_LINK as father, key, group_key
from t
start with CROSS_LINK is not null
CONNECT BY PRIOR kid = CROSS_LINK
order by GROUP_KEY
您可以将 CROSS_LINK 视为父列。 我的目标是在 GROUP_KEY .
中显示 KID 的所有父亲(层次结构中的直接和间接)此查询不以孤立的方式 运行 在 GROUP_KEY 中。您可以在 KID 'POD005' 的行为中观察到问题。在下图中,我突出显示了错误的行。
向您的 CONNECT BY
子句添加一个条件,以强制它只在与父行相同的 GROUP_KEY
中找到子项。
select level lvl, kid, CONNECT_BY_ROOT CROSS_LINK as father, key, group_key
from t
start with CROSS_LINK is not null
CONNECT BY PRIOR kid = CROSS_LINK
AND PRIOR group_key = group_key
order by GROUP_KEY