Question

I need some help querying hierarchical data. Here is the single, simple, table, where parent_id references id and may be null for root entries.

  create table edition (
      id           NUMBER(20),
      parent_id    NUMBER(20)
  );

For each record in the table I need to find the deepest child having maximum id. If a record has no children, then its own id should be returned. I tried by myself but failed using START WITH A.id = B.id where A and B are subqueries, looks like Oracle doesn't allow such joins.

Here is the sample data:

     id      parent_id
   ----------------------
      1        NULL
      2           1
      3           1
      4           1
      5           4
      6           5
      7           5

and a sample result

     id      result
   ----------------------
      1           7
      2           2
      3           3
      4           7
      5           7
      6           6
      7           7
Was it helpful?

Solution

I believe you want to try

create table tq84_edition (
  id        number primary key,
  parent_id number references tq84_edition
);

insert into tq84_edition values (  1, null);
insert into tq84_edition values (  2,    1);
insert into tq84_edition values (  3,    1);
insert into tq84_edition values (  4,    1);
insert into tq84_edition values (  5,    4);
insert into tq84_edition values (  6,    5);
insert into tq84_edition values (  7,    5);


with x (root, id, parent_id, lvl) as (
               select id    root,
                      id,
                      parent_id,
                      1 lvl
                from  tq84_edition
        UNION ALL
               select x.root  root,
                      tq84_edition.id,
                      tq84_edition.parent_id,
                      x.lvl + 1 lvl
                 from x,
                      tq84_edition
                where x.id = tq84_edition.parent_id
)
select  root, max(id) keep (dense_rank last order by lvl, id)
  from x
 group by root;

OTHER TIPS

Another approach I can think of, it's easier to port on other RDBMS: http://www.sqlfiddle.com/#!4/da0a3/19

with parent(root_node, child_of, parent_id, depth)  as
(
  select id, id, parent_id, 1
  from edition
  union all
  select p.root_node, e.id, e.parent_id, p.depth + 1
  from edition e
  join parent p on p.child_of = e.parent_id
)
select root_node, max(child_of)
from parent
where (root_node,depth) in
     (select root_node,max(depth) from parent group by root_node)
group by root_node
order by root_node

Output:

| ROOT_NODE | MAX(CHILD_OF) |
-----------------------------
|         1 |             7 |
|         2 |             2 |
|         3 |             3 |
|         4 |             7 |
|         5 |             7 |
|         6 |             6 |
|         7 |             7 |

Now, I'm loving Oracle (and http://sqlfiddle.com too), it's very concise. Now I know what's the use of MIN and MAX in KEEP DENSE_RANK. Whereas before I don't see any utility on explicitly specifying MIN/MAX in KEEP DENSE_RANK. Now I know it has a utility, if there's some ties on depth, you can see who is first and last in ties by using MIN and MAX.

e.g. http://www.sqlfiddle.com/#!4/da0a3/24

with parent(root_node, child_of, parent_id, depth)  as
(
  select id, id, parent_id, 1
  from edition
  union all
  select p.root_node, e.id, e.parent_id, p.depth + 1
  from edition e
  join parent p on p.child_of = e.parent_id
)
select root_node, 

  min(child_of) keep(dense_rank last order by depth) as first_in_deepest,
  max(child_of) keep(dense_rank last order by depth) as last_in_deepest

from parent
group by root_node;






| ROOT_NODE | FIRST_IN_DEEPEST | LAST_IN_DEEPEST |
--------------------------------------------------
|         1 |                6 |               7 |
|         2 |                2 |               2 |
|         3 |                3 |               3 |
|         4 |                6 |               7 |
|         5 |                6 |               7 |
|         6 |                6 |               6 |
|         7 |                7 |               7 |
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top