【问题标题】:Find customers in wrong level (hierarchical query)查找错误级别的客户(分层查询)
【发布时间】:2020-02-05 16:31:04
【问题描述】:

我正在尝试弄清楚如何找到错误级别的客户。 例如,所谓的“主要客户”在第 3 级,它不能在第 3 级或更高级别有“子客户”,但在第 2 级和第 1 级是。 如何创建这样一个揭示这种错误的分层查询? 一些样品,会非常好和受欢迎。

谢谢

【问题讨论】:

  • 没有样本数据和期望的结果,你的问题毫无意义。
  • edit您的问题(通过单击下面的edit 链接)并添加一些sample data 和基于该数据的预期输出。 Formatted textno screen shots。 (edit 您的问题 - 在 cmets 中不要邮政编码或其他信息)

标签: sql oracle hierarchy


【解决方案1】:

看这个例子:

从第一级开放层次结构开始,定义级别和叶或父级。如果您的客户级别 = 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

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多