Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.databases.oracle    |    Overblown overpriced overengineered SHIT    |    2,288 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 672 of 2,288    |
|    VC to Ray    |
|    Re: Traversing, while keeping a constant    |
|    14 Nov 03 01:06:35    |
      From: boston103@hotmail.com              Hello Ray,              Apparently you want to get the transitive closure over your tree.              Given :              create table t1(PARENT INT, CHILD INT);              insert into t1 values(null, 1);       insert into t1 values(1, 2);       insert into t1 values(1, 3);       insert into t1 values(2, 4);       insert into t1 values(2, 5);       insert into t1 values(3, 6);       insert into t1 values(3, 7);       insert into t1 values(5, 8);              In Oracle 9i., one way would be:              select        substr(path,2,instr(path,'/',1,2)-2) parent,        substr(path, instr(path,'/',-1,1)+1, length(path)-instr(path,'/',-1,1))       child,        distance       from (select sys_connect_by_path(child,'/') path, level-1 distance        from t1        connect by prior child=parent)       where instr(path,'/',1,2)!= 0              ... and another:              select p.child parent ,        c.child child,        level-1 distance       from t1 p, t1 c       where level > 1        connect by prior c.child = c.parent and prior p.child=p.child        start with p.child= c.child              PARENT CHILD DISTANCE       2 4 1       2 5 1       2 8 2       3 6 1       3 7 1       5 8 1       1 2 1       1 4 2       1 5 2       1 8 3       1 3 1       1 6 2       1 7 2              Both queries are not very efficient for large trees. Which one is worse is       left as an exercise for the reader ;)              In Oracle 8i, one has to write a stored procedure to perform BFS or DFS.       The stored procedure solution will be more efficient in 9i too since only       one tree traversal is needed.              Rgds.                     "Ray" |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca