473,473 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recursive SQL Question on breaking the infinite loop.

Hi,

I have created a recursive SQL Query in DB2 and it works fine until
some point in the tree where the data gets into infinite loop. Below
are some sample data from my relationship table.

Relationship Table
PARENT FIELD CHILD FIELD
AAA BBB
AAA CCC
AAA DDD
BBB EEE
BBB FFF
BBB AAA
CCC GGG
CCC AAA
DDD HHH
DDD AAA
EEE BBB
FFF BBB
GGG CCC
HHH DDD

If you notice the data the all the data are in cycle. That is because
the table has data from top to bottom tree as well as bottom to top
tree. I want to know how I can use the CYCLE Phrase to break the
infinite loop. I only am interested in gathering the data in the one
direction, say from Top Root to Bottom leaf ( I want to construct a
Tree from AAA all the way to GGG/HHH downwards.

Could you someone provide me with some samples or guide me the correct
direction. I reallly appreciate your time in reviewing this question
and responding to the post.
Aug 9 '08 #1
8 8288
Pivot_Tables wrote:
I have created a recursive SQL Query in DB2 and it works fine until
some point in the tree where the data gets into infinite loop. Below
are some sample data from my relationship table.

Relationship Table
PARENT FIELD CHILD FIELD
AAA BBB
AAA CCC
AAA DDD
BBB EEE
BBB FFF
BBB AAA
CCC GGG
CCC AAA
DDD HHH
DDD AAA
EEE BBB
FFF BBB
GGG CCC
HHH DDD

If you notice the data the all the data are in cycle. That is because
the table has data from top to bottom tree as well as bottom to top
tree. I want to know how I can use the CYCLE Phrase to break the
infinite loop. I only am interested in gathering the data in the one
direction, say from Top Root to Bottom leaf ( I want to construct a
Tree from AAA all the way to GGG/HHH downwards.

Could you someone provide me with some samples or guide me the correct
direction. I reallly appreciate your time in reviewing this question
and responding to the post.
Which version of DB2? Which platform and which from of recusrion are you
using (WITH UNION ALL vs. CONNECT BY)

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Aug 9 '08 #2
On Aug 9, 12:43*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On Aug 9, 4:17*pm, Pivot_Tables <atitbpa...@gmail.comwrote:


Hi,
I have created arecursive SQLQuery in DB2 and it works fine until
some point in the tree where the data gets into infinite loop. Below
are some sample data from my relationship table.
Relationship Table
PARENT FIELD * * * * * * * * * * *CHILD FIELD
AAA * * * * * * * * * * * * * * * * ** *BBB
AAA * * * * * * * * * * * * * * * * ** *CCC
AAA * * * * * * * * * * * * * * * * ** *DDD
BBB * * * * * * * * * * * * * * * * ** *EEE
BBB * * * * * * * * * * * * * * * * ** *FFF
BBB * * * * * * * * * * * * * * * * ** *AAA
CCC * * * * * * * * * * * * * * * * ** *GGG
CCC * * * * * * * * * * * * * * * * ** *AAA
DDD * * * * * * * * * * * * * * * * ** *HHH
DDD * * * * * * * * * * * * * * * * ** *AAA
EEE * * * * * * * * * * * * * * * * ** *BBB
FFF * * * * * * * * * * * * * * * * ** * BBB
GGG * * * * * * * * * * * * * * * * ** CCC
HHH * * * * * * * * * * * * * * * * ** *DDD
If you notice the data the all the data are in cycle. That is because
the table has data from top to bottom tree as well as bottom to top
tree.

Why? How can you tell whether A is the parent of B or the other way
around and what does it mean if someone deletes the tupel (AAA,BBB)
but leaves the tuple (BBB,AAA)?

[...]

/Lennart- Hide quoted text -

- Show quoted text -
Lenmart,

My application is such that if user creates a relationship like AAA ---
BBB it automatically creates BBB ---AAA relationship if you
looking at the tree bottoms up. I donot know why the application was
created in such a way but I just need to come up with a recursive SQL
where I go in one direction and to be more specific I want the tree
from root node to leaf node means downwards in the tree.

Just to answer a question, if someone deletes AAA,BBB it will
automatically delates BBB,AAA, so I donot worry about the data but I
just want to generate it downwords.

Serge, DB2 version is 9 with fix pack 4. Also, I am using common table
expression (CTE) using WITH phrash using UNION ALL. I thought the
CONNECT BY is for ORACLE. Does it work with DB2 as well. If yes, could
you tell me which one is better?

Thanks for looking into this and responding promptly.

Thanks!
Aug 10 '08 #3
On Aug 10, 5:37*am, Pivot_Tables <atitbpa...@gmail.comwrote:
[...]
>
Lenmart,

My application is such that if user creates a relationship like AAA ---BBB it automatically creates BBB *---AAA relationship if you

looking at the tree bottoms up. I donot know why the application was
created in such a way but I just need to come up with a recursive SQL
where I go in one direction and to be more specific I want the tree
from root node to leaf node means downwards in the tree.
For starters, how do you determine the root node? Is there some
additional info, for example an implicit ordering relation such that
AAA < CCC? What does your current query look like?

/Lennart

[...]
Aug 10 '08 #4
On Aug 10, 2:41*am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On Aug 10, 5:37*am, Pivot_Tables <atitbpa...@gmail.comwrote:
[...]
Lenmart,
My application is such that if user creates a relationship like AAA ---BBB it automatically creates BBB *---AAA relationship if you
looking at the tree bottoms up. I donot know why the application was
created in such a way but I just need to come up with a recursive SQL
where I go in one direction and to be more specific I want the tree
from root node to leaf node means downwards in the tree.

For starters, how do you determine the root node? Is there some
additional info, for example an implicit ordering relation such that
AAA < CCC? What does your current query look like?

/Lennart

[...]
Lennart,

Yes, I know the root node because the relationship table has a type
field for example, for AAA ---BBB record the relation type is AB and
I know the root node/record start with this type (AB). As I mentioned
earlier, the problem starts when I find the identical row in reverse
direction ( BBB ---AAA with relation type as BA ). Here is how my
query looks like.

WITH topdown_tree ( parent, child ) AS
(
select root.name, root.rel_name
from relationship AS root
where root.rel_typ = 'AB'

UNION ALL

select subroot.name, subroot.rel_name
from topdown_tree AS tt, relationship AS subroot
where tt.child = subroot.name
)
select parent, child
from topdown_tree;

Let me know if you need any other details. I really appreciate your
time and effort looking in to this and responding promptly.

Thanks !!!!!!!!!!

Aug 10 '08 #5

------------------------------ Commands Entered
------------------------------
WITH
/* Test Data */
relationship(parent, child) AS (
SELECT CAST( parent AS VARCHAR(25) )
, CAST( child AS VARCHAR(25) )
FROM (VALUES
('AAA', 'BBB')
,('AAA', 'CCC')
,('AAA', 'DDD')
,('BBB', 'EEE')
,('BBB', 'FFF')
,('BBB', 'AAA')
,('CCC', 'GGG')
,('CCC', 'AAA')
,('DDD', 'HHH')
,('DDD', 'AAA')
,('EEE', 'BBB')
,('FFF', 'BBB')
,('GGG', 'CCC')
,('HHH', 'DDD')
) td(parent, child)
)
/* End of Test Data */
,topdown_tree(k, leaf, path) AS (
SELECT DISTINCT
1
, child
, CAST( parent || ', ' || child AS VARCHAR(30) )
FROM relationship
/*
Start from AAA ---BBB
*/
WHERE parent = 'AAA'
AND child = 'BBB'
/**/
UNION ALL
/**/
SELECT k + 1
, child
, path || ', ' || child
FROM topdown_tree AS tt
, relationship AS subroot
WHERE k < 1000000
AND tt.leaf = subroot.parent
AND LOCATE(subroot.child, tt.path) = 0
)
SELECT path AS "Starting from AAA ---BBB"
FROM topdown_tree
WHERE k
= (SELECT MAX(k)
FROM topdown_tree
)
;
------------------------------------------------------------------------------

Starting from AAA ---BBB
------------------------------
AAA, BBB, EEE
AAA, BBB, FFF

2 record(s) selected.

------------------------------ Commands Entered
------------------------------
WITH
/* Test Data */
relationship(parent, child) AS (
SELECT CAST( parent AS VARCHAR(25) )
, CAST( child AS VARCHAR(25) )
FROM (VALUES
('AAA', 'BBB')
,('AAA', 'CCC')
,('AAA', 'DDD')
,('BBB', 'EEE')
,('BBB', 'FFF')
,('BBB', 'AAA')
,('CCC', 'GGG')
,('CCC', 'AAA')
,('DDD', 'HHH')
,('DDD', 'AAA')
,('EEE', 'BBB')
,('FFF', 'BBB')
,('GGG', 'CCC')
,('HHH', 'DDD')
) td(parent, child)
)
/* End of Test Data */
,topdown_tree(k, leaf, path) AS (
SELECT DISTINCT
1
, child
, CAST( parent || ', ' || child AS VARCHAR(30) )
FROM relationship
/**/
UNION ALL
/**/
SELECT k + 1
, child
, path || ', ' || child
FROM topdown_tree AS tt
, relationship AS subroot
WHERE k < 1000000
AND tt.leaf = subroot.parent
AND LOCATE(subroot.child, tt.path) = 0
)
SELECT path AS "Longest pathes"
FROM topdown_tree
WHERE k
= (SELECT MAX(tm.k)
FROM topdown_tree tm
)
;
------------------------------------------------------------------------------

Longest pathes
------------------------------
EEE, BBB, AAA, CCC, GGG
EEE, BBB, AAA, DDD, HHH
FFF, BBB, AAA, CCC, GGG
FFF, BBB, AAA, DDD, HHH
GGG, CCC, AAA, BBB, EEE
GGG, CCC, AAA, BBB, FFF
GGG, CCC, AAA, DDD, HHH
HHH, DDD, AAA, BBB, EEE
HHH, DDD, AAA, BBB, FFF
HHH, DDD, AAA, CCC, GGG

10 record(s) selected.

Aug 10 '08 #6
Modified slightly to meet your requirements.
------------------------------ Commands Entered
------------------------------
WITH
/* Test Data */
relationship(parent, child, rel_type) AS (
VALUES
('AAA', 'BBB', 'AB')
,('AAA', 'CCC', 'AC')
,('AAA', 'DDD', 'AD')
,('BBB', 'EEE', 'BE')
,('BBB', 'FFF', 'BF')
,('BBB', 'AAA', 'BA')
,('CCC', 'GGG', 'CG')
,('CCC', 'AAA', 'CA')
,('DDD', 'HHH', 'DH')
,('DDD', 'AAA', 'DA')
,('EEE', 'BBB', 'EB')
,('FFF', 'BBB', 'FB')
,('GGG', 'CCC', 'GC')
,('HHH', 'DDD', 'HD')
)
/* End of Test Data */
,topdown_tree(k, leaf, path) AS (
SELECT DISTINCT
1
, child
, CAST( parent || ' ---' || child AS VARCHAR(50) )
FROM relationship
/*
Start from AAA
*/
WHERE parent = 'AAA'
/**/
UNION ALL
/**/
SELECT k + 1
, child
, path || ' ---' || child
FROM topdown_tree AS tt
, relationship AS subroot
WHERE k < 1000000
AND tt.leaf = subroot.parent
AND LOCATE(subroot.child, tt.path) = 0
)
SELECT path AS "Start from AAA"
FROM topdown_tree
;
------------------------------------------------------------------------------

Start from AAA
--------------------------------------------------
AAA ---BBB
AAA ---CCC
AAA ---DDD
AAA ---BBB ---EEE
AAA ---BBB ---FFF
AAA ---CCC ---GGG
AAA ---DDD ---HHH

7 record(s) selected.
Aug 11 '08 #7
On Aug 10, 9:58*pm, Tonkuma <tonk...@fiberbit.netwrote:
Modified slightly to meet your requirements.
------------------------------ Commands Entered
------------------------------
WITH
/* Test Data */
*relationship(parent, child, rel_type) AS (
VALUES
* * * *('AAA', 'BBB', 'AB')
* * * ,('AAA', 'CCC', 'AC')
* * * ,('AAA', 'DDD', 'AD')
* * * ,('BBB', 'EEE', 'BE')
* * * ,('BBB', 'FFF', 'BF')
* * * ,('BBB', 'AAA', 'BA')
* * * ,('CCC', 'GGG', 'CG')
* * * ,('CCC', 'AAA', 'CA')
* * * ,('DDD', 'HHH', 'DH')
* * * ,('DDD', 'AAA', 'DA')
* * * ,('EEE', 'BBB', 'EB')
* * * ,('FFF', 'BBB', 'FB')
* * * ,('GGG', 'CCC', 'GC')
* * * ,('HHH', 'DDD', 'HD')
)
/* End of Test Data */
,topdown_tree(k, leaf, path) AS (
SELECT DISTINCT
* * * *1
* * *, child
* * *, CAST( parent || ' ---' || child AS VARCHAR(50) )
* FROM relationship
/*
Start from AAA
*/
*WHERE parent = 'AAA'
/**/
UNION ALL
/**/
SELECT k + 1
* * *, child
* * *, path || ' ---' || child
* FROM topdown_tree AS tt
* * *, relationship AS subroot
*WHERE k < 1000000
* *AND tt.leaf = subroot.parent
* *AND LOCATE(subroot.child, tt.path) = 0
)
SELECT path AS "Start from AAA"
* FROM topdown_tree
;
---------------------------------------------------------------------------*---

Start from AAA
--------------------------------------------------
AAA ---BBB
AAA ---CCC
AAA ---DDD
AAA ---BBB ---EEE
AAA ---BBB ---FFF
AAA ---CCC ---GGG
AAA ---DDD ---HHH

* 7 record(s) selected.
Thanks Tonkuma,

Just a quick question, what is the reason for putting k<1000000
criteria in the recursive query. If the Locate function avoids the
cycle/infinite loop, do you think we need to add the criteria
k<1000000 or not. The reason why I am asking that is because the data
I have is more than sample data I provided and it may go beyond
1000000 limit in the above query. Could you please clarify this.

As always thanks a lot for your time in looking into this.

Appreciated a lot!!!
Aug 11 '08 #8
Just a quick question, what is the reason for putting k<1000000
criteria in the recursive query. If the Locate function avoids the
cycle/infinite loop, do you think we need to add the criteria
k<1000000 or not.
To prevent the following message.
SQL0347W The recursive common table expression
"DB2ADMIN.TOPDOWN_TREE" may contain an infinite loop. SQLSTATE=01605
The reason why I am asking that is because the data
I have is more than sample data I provided and it may go beyond
1000000 limit in the above query. Could you please clarify this.
I can't beleive that the level of your data go beyond 1000000, even if
number of your data would go beyond 1000000.
Aug 11 '08 #9

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

Similar topics

2
by: Xavier Decoret | last post by:
I am reading the lines of a file, executing appropriate command if a pattern is found. One of the pattern can be a input command whose effect should be to #include the file (possibly recursively) ...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
8
by: Andrew Edwards | last post by:
The following function results in an infinite loop! After 5+ hours of debugging, I am still unable to decipher what I am incorrectly. Any assistance is greatly appreciated. Thanks in advance,...
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.