Home » SQL & PL/SQL » SQL & PL/SQL » Connect By to ensure full connection.
Connect By to ensure full connection. [message #20983] Thu, 04 July 2002 06:39 Go to next message
Durelli
Messages: 1
Registered: July 2002
Junior Member
I have an hierarchy of ids and parent id's e.g.

ID PID
1
2 1
3 2
4 1
6 5

What I want to do is to ensure that all nodes can trace a complete path back to the root node.

So in the above example report that node 6 cannot trace it's way back to node 1 (the root).

I'd expect to use connect by...prior...start with syntax, but finding 'missing links' isn't easy.

Please send any help or answers to dave.darrall@yahoo.com
Re: Connect By to ensure full connection. [message #20985 is a reply to message #20983] Thu, 04 July 2002 09:57 Go to previous message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
I am not very sure, what you are exactly looking for.
If you want to just find 'missing links' then
you can use something like this.
SQL> select * from tree;

        ID        PID
---------- ----------
         1
         2          1
         3          2
         4          1
         6          5
         8         15
         9          6
        10          3

8 rows selected.

This query gives u the regular hierarchial tree structure..
SQL> select
  2  lpad(' ',2*(level-1))||id org,pid   from  tree
  3     connect by prior id = pid
  4     start with pid is null;

ORG               PID
---------- ----------
1
  2                 1
    3               2
      10            3
  4                 1

And this gives the records that cannot be traced back..or Missing Links
SQL>  select id,pid from tree where
  2   id not in (
  3    select id    from  tree
  4       connect by prior id = pid
  5       start with pid is null);

        ID        PID
---------- ----------
         6          5
         8         15
         9          6

3 rows selected.
Previous Topic: hot to select those authors who live in the same zip area
Next Topic: Read a directory!
Goto Forum:
  


Current Time: Fri Apr 19 20:23:53 CDT 2024