Skip to content
Advertisement

How to flatten parent-child hierarchy from bottom up using CTE

for my first question on stack overflow I have the following problem, I’ve got an hierarchy table that looks as follows:

The data looks as follows:

the row with the wbe_node_type ‘leaf’ is the lowest level in the hierarchy, maximum depth is 8 levels deep. What I want is to flatten the hierarchy in one row from the bottom level (the ‘leaf’ node) to the top level.

My thought was to start to find al records with wbe_node_type = ‘leaf’ and find all the levels above it. This is easy to do with a temp-table. However, the tool I’m using to query this data doesn’t work with temp-tables. So I wanted to try it using a CTE.

When I’m looking for examples online, these always start at the top and work their way down. Is it possible to populate a CTE from the bottom starting with the lowest levels?

Advertisement

Answer

Not 100% sure on this but perhaps something like this.

Seems to work

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