Skip to content
Advertisement

SQL to retrieve parent-child relationship in parent-child order, from a self referencing table

I use the following query to retrieve the parent-child relationship data, from a table which is self referencing to the parent.

The table has the following fields: LocationId, FkParentLocationId, FkLocationTypeId, etc…

This works fine, but how I want to retrieve it is as follows:

What is currently gives is like:

How can I modify the above to get it in the order I want.

Advertisement

Answer

What about to append an ‘order’ field? This may be an approach:

Remember than Order by in an Insert is not allowed.

Take a look a sample

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