I have a structure (example, in brackets (id, parent_id) ):
- Directory 1 (1,0)
- Subdirectory 1 (44,1)
- Item 1 (83,44)
- Item 2 (98,44)
- Subdirectory 2 (46,1)
- Item 1 (8,46)
- Item 2 (9,46)
- Directory 2 (4,0)
- Subdirectory 1 (54,4)
- Item 1 (43,54)
- Item 2 (48,54)
- Subdirectory 2 (101,4)
- Item 1 (19,101)
- Item 2 (314,101)
In input I have list of id
If i don’t have id parent directory, but I have child id, then I must show all ancestry:
For example:
Input: 83, 46, 43, 48
Output:
- Directory 1 (1,0)
- Subdirectory 1 (44,1)
- Item 1 (83,44)
- Subdirectory 2 (46,1)
- Directory 2 (4,0)
- Subdirectory 1 (54,4)
- Item 1 (43,54)
- Item 2 (48,54)
I invented request:
SELECT *
FROM (SELECT distinct *
FROM Table a
START WITH N in (83, 46, 43, 48) CONNECT BY PRIOR N_PARENT = N) a
START WITH N_PARENT in 0 CONNECT BY PRIOR N = N_PARENT
ORDER SIBLINGS BY N
But in the big data is slow. Can I make request with one CONNECT BY?
http://sqlfiddle.com/#!4/34f2d/4