Sql 分层查询,帐户树
我一直使用分层查询来搜索父帐户或子帐户,通常让我们假设一个示例: 表帐户:Sql 分层查询,帐户树,sql,oracle,Sql,Oracle,我一直使用分层查询来搜索父帐户或子帐户,通常让我们假设一个示例: 表帐户: Account Parent 217518 217518 or null 304229 217518 424590 217518 378327 217518 491504 378327 234123 491504 因此,层次结构看起来: 现在,我得到了表“SHOW\u PARENT\u CHILD\u ACC”,我将在其中插入我想要显示的帐户。(我需要在分层查询中
Account Parent
217518 217518 or null
304229 217518
424590 217518
378327 217518
491504 378327
234123 491504
因此,层次结构看起来:
现在,我得到了表“SHOW\u PARENT\u CHILD\u ACC”,我将在其中插入我想要显示的帐户。(我需要在分层查询中使用它)
插入“显示父项子项acc(acc)”值(304229)
查询应返回所有帐户id,以便:
304229
424590
378327
217518
491504
234123
304229
424590
378327
217518
491504
234123
我也会的
插入显示父项cild acc(acc)值(234123)
查询应返回所有帐户id,以便:
304229
424590
378327
217518
491504
234123
304229
424590
378327
217518
491504
234123
换句话说,无论我将哪个帐户(父/子帐户或子帐户的子帐户)插入到show\u parent\u child\u acc表中。查询应返回整个帐户层次结构
现在,我有smth,比如:
WITH acc_to_delete( ID ) AS (
select case when parentaccount is null then accountid else parentaccount end from account where accountid in
(select acc from show_parent_child_acc)
)
SELECT accountid id_acc,case when parentaccount is null then accountid else parentaccount end parentaccount
FROM account p
START WITH
EXISTS( SELECT 'X'
FROM acc_to_delete w
WHERE p.accountid = w.ID
)
CONNECT BY accountid = PRIOR parentaccount
union
SELECT accountid id_acc,case when parentaccount is null then accountid else parentaccount end parentaccount
FROM account p
START WITH
EXISTS( SELECT 'X'
FROM acc_to_delete w
WHERE p.accountid = w.ID
)
CONNECT BY PRIOR accountid = parentaccount
但它并没有像预期的那样工作(在我放入SHOW\u PARENT\u CHILD\u ACC:217518..时工作正常)我将从查找根id(子查询“root”)开始,然后创建典型的层次结构:
with
tmp as (select acc from show_parent_child_acc ),
root as (
select max(acnt) keep (dense_rank last order by lvl) root
from (
select nvl(parentaccount, accountid) acnt, level lvl
from account a cross join tmp
connect by nocycle prior parentaccount = accountid
start with accountid = acc or parentaccount = acc ))
select accountid from account cross join root
connect by nocycle prior accountid = parentaccount
start with parentaccount = root.root
union select root from root
测试数据和输出:
create table account (accountid number(8), parentaccount number(8));
insert into account values (217518, 217518);
insert into account values (304229, 217518);
insert into account values (424590, 217518);
insert into account values (378327, 217518);
insert into account values (491504, 378327);
insert into account values (234123, 491504);
create table show_parent_child_acc (acc number(8));
insert into show_parent_child_acc values (234123);
AccountID
---------
217518
234123
304229
378327
424590
491504
进一步修改原始查询,如下所示
WITH acc_to_delete( ID ) AS (
select case when parentaccount is null then accountid else parentaccount end from account where accountid in
(select acc from show_parent_child_acc)
)
SELECT DISTINCT accountid id_acc, case when parentaccount is null then accountid else parentaccount end parentaccount
FROM account START WITH parentaccount in
(
SELECT case when parentaccount is null then accountid else parentaccount end parentaccount
FROM account p
START WITH
EXISTS( SELECT 'X'
FROM acc_to_delete w
WHERE p.accountid = w.ID
)
CONNECT BY nocycle accountid = PRIOR parentaccount
UNION
SELECT case when parentaccount is null then accountid else parentaccount end parentaccount
FROM account p
START WITH
EXISTS( SELECT 'X'
FROM acc_to_delete w
WHERE p.accountid = w.ID
)
CONNECT BY nocycle PRIOR accountid = parentaccount
)
CONNECT BY NOCYCLE parentaccount = PRIOR accountid
它主要收集所有相应的家长,然后找到他们的孩子。很高兴我能帮忙。这里的问题是根值没有明确定义。在您的示例中,217518根本不存在,或者与id具有相同的父项,或者父项为null。我试图编写处理所有情况的查询,这就是出现
nocycle
的原因。我向fajnie pomóc rodakowi,pozdrawiam;-)