В зависимости от вашего использования столбца LEVEL (согласно моему комментарию).
Информация об иерархических запросах Oracle: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
Возвращает то, что вы запрашиваете, если LEVEL - Oracleпсевдостолбец:
WITH t AS (SELECT 0 AS parent,
1 AS id,
'toplevel' AS text FROM DUAL
UNION
SELECT 1 AS parent,
2 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 1 AS parent,
3 AS id,
'sumthin else' AS text FROM DUAL
UNION
SELECT 1 AS parent,
4 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 0 AS parent,
7 AS id,
'toplevel2' AS text FROM DUAL
UNION
SELECT 7 AS parent,
8 AS id,
'secondlevel' AS text FROM DUAL
UNION
SELECT 7 AS parent,
9 AS id,
'anothersecondlevel' AS text FROM DUAL
)
SELECT UNIQUE
level,
parent,
id,
text
FROM t
START WITH text = 'foo'
CONNECT BY PRIOR parent = id
ORDER BY parent;
Возвращает:
LEVEL PARENT ID TEXT
2 0 1 toplevel
1 1 2 foo
1 1 4 foo
Если LEVEL - это столбец в вашей таблице, то:
WITH t AS (SELECT 0 AS tlevel,
0 AS parent,
1 AS id,
'toplevel' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
2 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
3 AS id,
'sumthin else' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
1 AS parent,
4 AS id,
'foo' AS text FROM DUAL
UNION
SELECT 0 AS tlevel,
0 AS parent,
7 AS id,
'toplevel2' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
7 AS parent,
8 AS id,
'secondlevel' AS text FROM DUAL
UNION
SELECT 1 AS tlevel,
7 AS parent,
9 AS id,
'anothersecondlevel' AS text FROM DUAL
)
SELECT UNIQUE
tlevel,
parent,
id,
text
FROM t
START WITH text = 'foo'
CONNECT BY PRIOR parent = id
ORDER BY parent;
Возвращает:
TLEVEL PARENT ID TEXT
0 0 1 toplevel
1 1 2 foo
1 1 4 foo
Надеюсь, это поможет ...