Hierarchy 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.


Hierarchy SQL Expand / Collapse
Author
Message
Posted 8/22/2006 6:53:37 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 10/4/2006 3:39:00 PM
Posts: 3, Visits: 1
Hello Gurus,

Is there an equivalent statement for CONNECT BY..PRIOR in Oracle. I'm trying to simulate a hierarchy in Teradata SQL and i'm using the WITH RECURSIVE query. It is not solving my requirement as it gives me

1,2
2,3
2,4
4,5
4,6

where the first column being the parent and the second- its child.

I need

1,2
1,3
1,4
1,5
1,6
2,3
2,4
2,5
2,6
4,5
4,6

Can anybody suggest a method to achieve this using ANSI/teradata SQL.

Thanks,


Hari
Post #4962
Posted 8/24/2006 2:58:28 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2 days ago @ 3:41:05 AM
Posts: 522, Visits: 264
Hi Hari,
of course it's possible, you just have to code it the right way :-)
Maybe show us what you wrote so far...

Returning the hierarchy of Teradata databases & users as an example:

WITH RECURSIVE parent (databasename, path, level) AS
(SELECT databasename, databasename(varchar(3000)), 0 (byteint)
FROM dbc.databases d
WHERE databasename = ownername
UNION ALL
SELECT c.databasename, p.path || '.' || trim(c.databasename),level + 1
FROM dbc.databases C
,parent P
WHERE c.ownername = p.databasename
and c.databasename <> c.ownername
and level < 10
)
SELECT
databasename,
level,
substring(' ' from 1 for level * 2) || databasename,
path
FROM parent
order by path;

There's a good article from IBM covering porting Connect By to With:
http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0510rielau/

Dieter
Post #4973
Posted 8/24/2006 9:01:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 10/4/2006 3:39:00 PM
Posts: 3, Visits: 1
Hi Dieter,

Thanks for your Reply.

This is the recursive query i wrote

DATABASE EDW;
WITH RECURSIVE HARI_TMP
(mgr,emp,LEVEL) AS
(
SELECT mgr,emp,0
FROM EMPDATA
WHERE JOB_TITLE='PRESIDENT'
UNION ALL
SELECT EMPDATA.mgr,
EMPDATA.emp,
HARI_TMP.LEVEL+1
FROM EMPDATA,HARI_TMP
WHERE HARI_TMP.emp=EMPDATA.mgr
AND HARI_TMP.LEVEL<10
)
SELECT *
FROM HARI_TMP
ORDER BY 3,1,2;

and it returns the following result

1,2
2,3
2,4
4,5
4,6

I'm working on the query you had sent me.

Thanks,
Hari




Hari
Post #4974
Posted 8/25/2006 4:26:46 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2 days ago @ 3:41:05 AM
Posts: 522, Visits: 264
Hi Hari,
i don't know your data, but it looks like you want to calculate the Transitive Closure, i.e. all existing paths.

For all rows it's probably:
WITH RECURSIVE HARI_TMP
(mgr,emp,LEVEL) AS
(
SELECT mgr,emp,0
FROM EMPDATA
UNION ALL
SELECT HARI_TMP.mgr,
EMPDATA.emp,
HARI_TMP.LEVEL+1
FROM EMPDATA,HARI_TMP
WHERE HARI_TMP.emp=EMPDATA.mgr
AND HARI_TMP.LEVEL<10
)
SELECT distinct (?) mgr,emp
FROM HARI_TMP
ORDER BY 1,2;


For a given start row (JOB_TITLE='PRESIDENT') i'd calculate a path and the join on it:
WITH RECURSIVE HARI_TMP
(mgr,emp,LEVEL, path) AS
(
SELECT mgr,emp,0,cast(emp as varchar(500))
FROM EMPDATA
WHERE JOB_TITLE='PRESIDENT'
UNION ALL
SELECT EMPDATA.mgr,
EMPDATA.emp,
HARI_TMP.LEVEL+1,
path || '.' || trim(empdata.emp)
FROM EMPDATA,HARI_TMP
WHERE HARI_TMP.emp=EMPDATA.mgr
AND HARI_TMP.LEVEL<10
)
SELECT t1.emp,t2.emp
FROM HARI_TMP t1 , HARI_TMP t2
where t2.path like '%' || t1.path || '%'
and t1.emp <> t2.emp
ORDER BY 1,2;


Dieter
Post #4979
Posted 9/3/2006 11:47:52 PM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 10/7/2008 6:20:30 PM
Posts: 136, Visits: 11
Dieter

I have not used much of recursive SQL's. But from what I came to know from manuals it may have its performance penalties (redistribution and other TD stuffs) if we do iterate thru a non-PI column (I believe).

I guess we may have to weigh and measure consideration before we trying this exciting features.


Vinay
Post #5051
« 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 3:09am

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.063. 7 queries. Compression Disabled.