Skip to content
Advertisement

Get the immediate parent child relation in a table

I have a table with following value

I need the output data from the above in the following format :

Thanks in advance for any guidance on this.

EDIT : I used the while loop approach to achieve this but trying to avoid the while loop.

The while loop logic worked if proper sequence of ranklvl is there for a given record set like 1->2->3. But it did not work if data is in following way :

The following expected result is not coming by the while loop logic.

Advertisement

Answer

I think you want a self-join:

If there are gaps in the ordering column, you can enumerate first:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement