Teradata Recursive SQL
Teradata Teradata Discussion Forums Teradata.com Discussion Forum
Visit Teradata.com
Home       Guidelines    Member List
Welcome Guest ( Login | Register )
        


This online forum is for user-to-user discussions of Teradata products, and is not an official customer support channel for Teradata. If you require direct assistance, please contact Teradata support.


Teradata Recursive SQL Expand / Collapse
Author
Message
Posted 5/8/2008 1:02:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 5/8/2008 5:25:30 AM
Posts: 8, Visits: 6
Hi All,

I'm new to teradata and your help here would be greatly appreaciated.I need a recursive SQL to find the child-parent relationship in a table.For example,

Child Parent

B A
C B
D B
G C
J A

Now i need to recursively find out the parents until a node does not have any parents(I need to do this search recursively for 5 levels)

For instance,If i want to trace the parent of child G...it must be like G-->C-->B-->A
If i want to know the parent of G,it must traverse like D-->B-->A.

Regards,
Kumar
Post #11379
Posted 5/8/2008 2:01:25 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 6/25/2008 12:24:48 AM
Posts: 425, Visits: 389
I haven't tested... but something like this should work ...

WITH RECURSIVE RECTBL(CHLD, PRNT, LVL)
AS
(

SELECT CHILD, PARENT, 1(INTEGER)
FROM MYTBL
WHERE CHILD = 'G'

UNION ALL

SELECT MYTBL.CHILD, MYTBL.PARENT, RECTBL.LVL + 1
FROM RECTBL INNER JOIN MYTBL
ON RECTBL.PRNT = MYTBL.CHILD
AND RECTBL.LVL <= 5

)
SELECT CHLD, PRNT
FROM RECTBL
ORDER BY LVL
;
Post #11380
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 ( 0 guests, 0 members, 0 anonymous members )
No members currently viewing this topic.


All times are GMT -5:00, Time now is 6:51pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.078. 10 queries. Compression Disabled.