473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Traversing, while keeping a constant

Ray
I have a real puzzle, I figured someone on here could help. I have a
table that tracks all parents and children. I would like to set
iterate over all entities where I set a variable to equal a root
parent and return all relations disregarding why the relation exists.
Essentially here is what I would like to do however, this won't work
for obvious reasons:

select parent, (select child from table
start with parent = (select parent from table)
connect by parent = prior child)
from table

so instead of
1,2
1,3
3,4
3,5

I need my result to be used in a view where
1,2
1,3
1,4
1,5
3,4
3,5

Anyone know of a way to do this? I know that I can use a cursor and
keep track of the root parent while using a cursor to track all
relations for the root parent and place that into a function, but this
will be constantly growing and I can't process this logic everytime
there is an update.
Thanks for any insight...
Ray
Jul 19 '05 #1
1 2726
VC
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" <rb*******@hotmail.com> wrote in message
news:cb**************************@posting.google.c om...
I have a real puzzle, I figured someone on here could help. I have a
table that tracks all parents and children. I would like to set
iterate over all entities where I set a variable to equal a root
parent and return all relations disregarding why the relation exists.
Essentially here is what I would like to do however, this won't work
for obvious reasons:

select parent, (select child from table
start with parent = (select parent from table)
connect by parent = prior child)
from table

so instead of
1,2
1,3
3,4
3,5

I need my result to be used in a view where
1,2
1,3
1,4
1,5
3,4
3,5

Anyone know of a way to do this? I know that I can use a cursor and
keep track of the root parent while using a cursor to track all
relations for the root parent and place that into a function, but this
will be constantly growing and I can't process this logic everytime
there is an update.
Thanks for any insight...
Ray

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Timin Uram | last post by:
I'm not sure if this forum is the correct place to post this, but I couldn't think of any other group. I would really appreciate any help you could give me. FINAL GOAL OF MY APPLICATION:...
1
by: lothar | last post by:
i want to traverse a set of messages in a Yahoogroups group from a Python program. to get to the messages of the group, one must log in. this presents, i think, two problems, 1) handling the...
1
by: w.p. | last post by:
Hello! I want change default tab traversing in my app. But i don't know how to do it :( Belowe i include simple example - i want change default tab order: radiobutton "mode11" -> radiobutton...
2
by: Jim Cobban | last post by:
I am using Xerces to read an XML file and load it into a DOM so I can update it and subsequently serialize the updated DOM. The problem I have is that as I traverse the DOM I would like to inform...
3
by: Plamen Valtchev | last post by:
This is my problem: From JavaScript I want to find the list of all defined/loaded JavaScript functions/objects/names within the current scope (html page in a browser). the page could contain...
17
by: No One | last post by:
Is there a way to keep a control centered inside a form without having to recalculate everytime the form is resized?
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
30
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.