Skip to content
Advertisement

Postgres get root of self-referential table

Given the table:

If I get a name from the table (and thus the Pkey), how can I get the name of it’s “root”-level parent: the name where parent is NULL which is referenced to (indirectly) by the given name.

Say I have these values (I don’t know which values I will get):

The function/whatever solution should return Root 1 for Root 1, Child 1 and Sub-child 1; and should return Root 2 for Root 2 and Child 2.

How can I achieve this? Note: It really has to be Postgres….

Advertisement

Answer

You need a recursive CTE. The syntax looks like:

Here is a db<>fiddle.

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