查找错误级别的客户(分层查询)

问题描述 投票:0回答:1

我试图找出如何找到错误级别错误的客户。例如,所谓的“主要客户”在级别3中,它不能在级别3或更高级别中具有“子客户”,但是在级别2和1中可以。如何创建一个显示此类错误的层次查询?一些示例,非常好,欢迎您。

谢谢

sql oracle hierarchy
1个回答
0
投票

看这个例子:

由于第一级开放层次结构,请定义级别和叶子或父级。如果您的客户级别= 3并且是父级,则找到他]

    with tbl as (
select '12' id, null parent_id, 'main customer' status from dual
union all
select '13' id, null parent_id, 'main customer' status from dual
union all
select '131' id, 13 parent_id, 'main customer' status from dual
union all
select '121' id, 12 parent_id, 'main customer' status from dual
union all
select '1211' id, 121 parent_id, 'main customer' status from dual
union all
select '1212' id, 121 parent_id, 'main customer' status from dual
union all
select '12111' id, 1211 parent_id, 'main customer' status from dual
union all
select '12121' id, 1212 parent_id, 'main customer' status from dual)
select * from  
(select tbl.*, level lvl, connect_by_isleaf leaf 
from tbl
start with tbl.parent_id is null 
connect by prior id = parent_id) b 
where b.lvl = 3
AND b.status = 'main customer'
AND b.leaf = 0

输出:

1211    121 main customer   3   0
1212    121 main customer   3   0
© www.soinside.com 2019 - 2024. All rights reserved.