Hi,
I am not so well versed in Oracle analytic functions and am having trouble with a sql.
I have a tree table storing heirarchies upto 4 levels.
create table FAMILY_TREE
(
TREE_ID NUMBER not null,
PARENT_ID NUMBER,
NAME VARCHAR2(100)
);
I know that using a CONNECT BY PRIOR, I can find out who the previous ancestar is.
select
f.tree_id TreeId,
f.parent_id ParentId,
f.Name Name
from family_tree f
connect by
prior f.tree_id = f.parent_id
start with f.parent_id is null
I need to display all the ancestars in a single line (in different columns), which is where I am stumped.
I need the output as :
Joe Jack Harry Rick Ned
Bascially, I need to display in a single row, all the ancestars.
I tried to use joins but it was very very slow due to the fact that I had to perform multiple joins.
Can anyone please point me in the right direction?
Data:
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (1, null, 'Joe');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (2, 1, 'Jack');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (3, 2, 'Harry');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (4, 3, 'Rick');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (5, 4, 'Ned');
I am not so well versed in Oracle analytic functions and am having trouble with a sql.
I have a tree table storing heirarchies upto 4 levels.
create table FAMILY_TREE
(
TREE_ID NUMBER not null,
PARENT_ID NUMBER,
NAME VARCHAR2(100)
);
I know that using a CONNECT BY PRIOR, I can find out who the previous ancestar is.
select
f.tree_id TreeId,
f.parent_id ParentId,
f.Name Name
from family_tree f
connect by
prior f.tree_id = f.parent_id
start with f.parent_id is null
I need to display all the ancestars in a single line (in different columns), which is where I am stumped.
I need the output as :
Joe Jack Harry Rick Ned
Bascially, I need to display in a single row, all the ancestars.
I tried to use joins but it was very very slow due to the fact that I had to perform multiple joins.
Can anyone please point me in the right direction?
Data:
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (1, null, 'Joe');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (2, 1, 'Jack');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (3, 2, 'Harry');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (4, 3, 'Rick');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (5, 4, 'Ned');