473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2736
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,i nstr(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*******@hotm ail.com> wrote in message
news:cb******** *************** ***@posting.goo gle.com...
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
2196
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: Building a friendster clone for a very large organization (10,000 people). I am using Php/Mysql. SETUP OF SYSTEM: (it may help if you are familiar with...
1
2921
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 form element for the login, which has a javascript submit routine, 2) keeping login state with cookies.
1
4187
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 "mode31" -> button OK I can't find any option, flag, or another way.
2
2853
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 the user of exceptional conditions in the file. However I cannot find any way while doing a DOM traversal to determine the line number that a...
3
1943
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 many included javascript with the script tag : <script language="javascript" src="XXX/Script.js"></script>
17
6869
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
4865
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 next for null).Is there any way to find the length of linked list in c.My need is to traverse from the end to 5th node Regards, Mani
19
2344
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 chunk of memory that it can use for it's own purposes. But on the "library" side, I want to be able to do some book-keeping for each memory block...
30
4256
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
1521
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 query. The query comes from a table which picks values from another table. When I try traversing the query through TAB button, I get the following...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.