So, since I struggled to find an accurate title, I think a detailled shema will be much more understandable.
I have this table PROGRAM that I will reduce to 3 fields for simplicity:
ID |NAME |ID_ORIGINAL_PROGRAM
1 |name_1 |
2 |name_2 |1
3 |name_3 |1
4 |name_4 |2
5 |name_5 |3
6 |name_6 |
7 |name_7 |6
I'm trying to find a query that will allow me, with any ID as parameter to gather all the related programs to this id. And I need to be able to send a parameter than does not necessarily has to be the "father" id of the hierarchy.
For example, if parameter ID is 1, then results will be:
ID
2
3
4
5
If parameter ID is 4, then the results will be:
ID
1
2
3
5
It seems like I'm missing some kind "loop" logic that I can't clearly identify. I looked up at "CONNECT BY PRIOR" but was not able to grasp the concept enough to understand how to deploy it.
Edit: So it seems I found a way through:
SELECT ID
FROM PROGRAM
START WITH ID = 67256
CONNECT BY NOCYCLE ID_ORIGINAL_PROGRAM = PRIOR ID
OR ID = PRIOR ID_ORIGINAL_PROGRAM
order by ID
I'm a bit concerned by the performances though (it takes 1 second to perform)