【问题标题】:Hierarchical query to find root and return singular path查找根并返回奇异路径的分层查​​询
【发布时间】:2018-12-10 21:17:54
【问题描述】:

我正在使用一个不是我创建的黑盒数据库,我也无法编辑。此外,此选择查询需要在 jasper 报告中运行,因此我无法编写存储过程。

我有一个库存系统,它使用父/子映射表并在每次库存状态发生变化时生成新键。我正在使用 2 个表,这些是重要字段和简化查询:

allinv 包含以下字段: 管理员 输入键

包含字段的 pclink(对应于上面 allinv 的 invkeys): 父母 孩子

以下查询适用于一个库存项目并按预期显示进度,但它需要知道根并提供主密钥。

然后我的问题是,如果我需要使用这种结构返回所有库存项目(删除 where 子句),有没有办法自动检测根元素并设置为“开始于”或类似的东西所以我可以将其用作分析窗口查询的子查询,以沿途提取根、叶和状态?

select
  ai.invkey AS sxkey,
  stl.parent as parent,
  stl.child as child,
  level as lvl,
  CONNECT_BY_ISLEAF as IsLeaf,
  CONNECT_BY_ROOT(STL.parent) as root
from eprog.allinv ai
  left join eprog.pclink stl
    on ai.invkey = stl.parent
where invmasterkey = 'SA3010401'
start with stl.parent = '#gRR5'
connect by nocycle prior stl.child = stl.parent;

【问题讨论】:

  • 为什么只有提供根密钥和主密钥才有效?一些有代表性的样本数据和预期输出会有所帮助。

标签: oracle oracle11g hierarchy hierarchical-data hierarchical


【解决方案1】:

我不确定我是否完全理解您的问题,但您当然可以:

START WITH stl.parent IS NULL

用这些行开始你的分层查询。

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多