Suppose I had the following table in my oracle DB:
ID: Name: Parent_ID: 123 a 234 345 b 123 234 c 234 456 d 345 567 e 567 678 f 567
And what I would like to do is find for each ULTIMATE parent ID ID ULTIMATE parent ID (described as a string, that when you go up, recursively, based on the Parent_ID string, where you finally get this ID = Parent_ID ).
So, for example, 345 parent 123 and 123 parent 234 and 234 parent 234 (which means that this is the top of the chain), so 345 is the ultimate parent 234 - I hope this makes sense ...
So, my result should look like this:
ID: Name: Ult_Parent_ID: Ult_Parent_Name: 123 a 234 c 345 b 234 c 234 c 234 c 456 d 234 c 567 e 567 e 678 f 567 e
I just learned about Oracle Connect By statuses today, so this is completely new to me, but I imagine that my query should look like SOMETHING as follows:
SELECT ID, Name, Parent_ID as Ult_Parent_ID, (SELECT Name from MyTable t2 WHERE t2.ID = t1.Parent_ID) as Ult_Parent_Name FROM MyTable t1 CONNECT BY PRIOR Parent_ID = ID;
Now, as I said, this is my first hit in this kind of SQL - THIS DOES NOT WORK (I get the following error [1]: ORA-01436: CONNECT BY loop in user data and selects the table name in the SQL editor), and I also I donβt know where / how to use the START WITH clause for such requests, but the logic of this seems to me to be correct.
Please help / help point me in the right direction !!!
Thanks!!!