Используя эту схему (это может быть неправильно):
CREATE TABLE family (
family_ID NUMBER,
seq_ID NUMBER,
name VARCHAR2(100),
ref_parent NUMBER,
PRIMARY KEY (seq_id),
FOREIGN KEY (ref_parent) REFERENCES family
);
INSERT INTO family VALUES (55, 1, 'John', NULL);
INSERT INTO family VALUES (55, 2, 'James', 1);
INSERT INTO family VALUES (55, 3, 'Mary', 2);
INSERT INTO family VALUES (55, 4, 'Jim', 3);
INSERT INTO family VALUES (65, 5, 'Mark', NULL);
INSERT INTO family VALUES (65, 6, 'Gen', 5);
INSERT INTO family VALUES (65, 7, 'Mary', 6);
INSERT INTO family VALUES (65, 8, 'Jim', 7);
Решение (на основе комментария Kaushik Nayak ) будет выглядеть примерно так:
SELECT f.family_id, f.seq_id, f.name, f.ref_parent, SUBSTR(SYS_CONNECT_BY_PATH(f.name, ' > '), 4) hierarchy
FROM family f
CONNECT BY PRIOR f.seq_id = f.ref_parent
START WITH f.ref_parent IS NULL
ORDER SIBLINGS BY f.seq_id
Выход:
FAMILY_ID | SEQ_ID | NAME | REF_PARENT | HIERARCHY
----------+--------+-------+------------+--------------------------
55 | 1 | John | (null) | John
55 | 2 | James | 1 | John > James
55 | 3 | Mary | 2 | John > James > Mary
55 | 4 | Jim | 3 | John > James > Mary > Jim
65 | 5 | Mark | (null) | Mark
65 | 6 | Gen | 5 | Mark > Gen
65 | 7 | Mary | 6 | Mark > Gen > Mary
65 | 8 | Jim | 7 | Mark > Gen > Mary > Jim
См. Документы Oracle по функции SYS_CONNECT_BY_PATH и этой SQL Fiddle , чтобы увидеть, как она работает.