【问题标题】:Root level node is not coming in result根级节点未出现结果
【发布时间】:2019-08-23 15:34:03
【问题描述】:

我是 Oracle 分层查询的新手。我有一张桌子,有以下数据。

Table Data Result Data 我的问题是为什么结果中不包含 parent id(100) ? 以下是查询。

select id, lpad(' ',4*(LEVEL - 1)) || CHILD CHILD, LEVEL
from temp
START WITH PARENT = 100
CONNECT BY  PARENT = PRIOR CHILD;

问候, 布山

【问题讨论】:

  • 不建议将数据和结果(或代码)作为图像发布。许多人无法访问图片网站,因此您限制了可以帮助您的人。它还增加了人们重现您的问题所需的工作量,从而进一步减少了准备提供帮助的人。
  • 您正在选择child 列,而100 未出现在此列中。

标签: sql oracle hierarchy hierarchical-data


【解决方案1】:

如果要在输出中包含起始值,则使用 union all:

select 0 id, '100' child, 0 lvl from dual
union all
select id, lpad(' ', 4 * level ) || child, level 
  from temp
  start with parent = 100
  connect by  parent = prior child

或递归 CTE:

with c(id, child, lvl) as (
    select 0, '100', 0 from dual 
    union all
    select t.id, lpad(t.child, (c.lvl + 2) * 4, ' '), c.lvl + 1 
      from c join temp t on t.parent = c.child)
search depth first by id set seq
select id, child, lvl from c;

或先将其添加到源数据中:

select id, lpad(' ', 4 * (level-1) ) || child child, level 
  from (select id, parent, child from temp union all
        select null, null, 100 from dual )
  start with child = 100
  connect by  parent = prior child

demo with all queries

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多