I have a table with the following columns: group_id, parent_id, name
In this table, parent_id is the group_id of another record. There is a parental relationship with children from 1 to N. This forms a hierarchy with only one top-level group that has NULL for parent_id. There may be arbitrary depth, but in practice my hierarchy should not exceed 20 levels.
I would like to get every ancestor (parent parent, etc.) of a group with a given group_id. I am concerned about the specific way to return it.
I use MS SQL 2005, but I am also interested in solutions using other RDBMSs.
I found several similar questions, but they all seem to be broken down into recursion, cyclic or nested sets. I cannot use nested sets because I cannot change the data structure. I would like to avoid recursion or loop where possible, or at least understand why this is not possible.
Here are some questions I found while researching this:
How to choose parent identifiers
Sql recursion without recursion
source
share