The problem is that you are not dealing with strict hierarchies; you are dealing with directed graphs where some graphs have loops. Note that your nbgroup # 1 does not have a canonical root - it could be a, b or c due to the circular reference from ca.
The main way to deal with this is to think in terms of graph methods, not recursion. In fact, an iterative approach (not using CTE) is the only solution I can come up with in SQL. The basic approach is explained here .
Here is an SQL script with a solution that applies to both loops and the case with a common sheet. Note that it uses iteration (with failover to prevent runaway processes) and table variables to work; I do not think it will cost. Also note the changed sample data (ag changed to ah, explained below).
If you delve into SQL, you will notice that I have changed some key points from the solution indicated in the link. This solution concerned non-oriented edges, while your edges are directed (if you used non-oriented edges, the entire set of samples is the only component due to the ag compound).
This is the reason why I changed ag to ah in my sample data. Your problem specification is simple as long as the common nodes are separated; what specification i encoded. In this case, ah and gh can easily be tied to their own components, because we are worried about parental reachability (even given cycles).
However, when you have common branches, it is not clear what you want to show. Consider the relation ag: in this case, gh can exist in any component (agh or fgh). You put him in the second, but maybe he was the first, right? This ambiguity is why I did not try to solve it in this decision.
Edit: To be clear, in my solution above, if common marriages are found, it treats the whole set as one component. Not what you described above, but it will need to be changed after clarifying the problem. Hope this brings you closer.
Dominic P
source share