dbTalk Databases Forums  

Selecting All Parents in a Path Enumerated Tree

comp.databases.oracle.server comp.databases.oracle.server


Discuss Selecting All Parents in a Path Enumerated Tree in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ones Self
 
Posts: n/a

Default Selecting All Parents in a Path Enumerated Tree - 10-29-2004 , 05:01 PM






Hi,

I have a tree structure which is maintained through the use of a path
enumerated column:

CREATE TABLE items (
item_id NUMERIC NOT NULL,
path VARCHAR2(64) NOT NULL
);

The path is is a colon separated list of ids of the nodes of the tree.
So, for example, in this structure:

0 -> 1 -> 2 -> 3 -> 4

item id 4 would have a path of '0:1:2:3' (0 is the root of all
items, and does not actually exist). Notice that the path does not
include the item's own id.

I would like to select all of the items in a given item's path:

SELECT id, path FROM items WHERE id IN (PATH_TO_IDS(path));

or maybe:

SELECT id, path FROM items WHERE PATH_EQUALS(id, path));

or maybe something else altogether. This should return:

ITEM_ID PATH
------- -------
1 0
2 0:1
3 0:1:2
4 0:1:2:3

Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: Selecting All Parents in a Path Enumerated Tree - 10-30-2004 , 09:42 AM







"Ones Self" <nutgg001 (AT) sneakemail (DOT) com> a écrit dans le message de
news:a2122d77.0410291401.6891b9f (AT) posting (DOT) google.com...
Quote:
Hi,

I have a tree structure which is maintained through the use of a path
enumerated column:

CREATE TABLE items (
item_id NUMERIC NOT NULL,
path VARCHAR2(64) NOT NULL
);

The path is is a colon separated list of ids of the nodes of the tree.
So, for example, in this structure:

0 -> 1 -> 2 -> 3 -> 4

item id 4 would have a path of '0:1:2:3' (0 is the root of all
items, and does not actually exist). Notice that the path does not
include the item's own id.

I would like to select all of the items in a given item's path:

SELECT id, path FROM items WHERE id IN (PATH_TO_IDS(path));

or maybe:

SELECT id, path FROM items WHERE PATH_EQUALS(id, path));

or maybe something else altogether. This should return:

ITEM_ID PATH
------- -------
1 0
2 0:1
3 0:1:2
4 0:1:2:3
Something like:

def baseId=4
select id, path
from items, (select path basePath from items where id=&baseId)
where basePath like '%:'||id||':%' or basePath like id||':%' or basePath like '%:'||id
union all /* if you also want the BaseId full path */
select id, path where id = &baseId
order by 2
/

--
Regards
Michel




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.