Create a new recursive CTE called hierarchy
. The content of the boss
CTE from the template should become the anchor member. Then, use UNION ALL
and modify the external query from the template so that it uses recursion. Remember that the id of the superior must match the id that you got from the previous recursive step. Thanks to it, as the number of subordinates in our table is finite, the query will eventually stop. In other words, the termination check is not explicitly provided.
In the outer query, simply select all the information from your recursive CTE.
Don't forget that you start your recursive queries with WITH RECURSIVE
.