I have following different tables
LeafChilds
ChildId, ChildName
1, Child1
2, Child2
Group
GroupId, GroupName
1, Group1
2, Group2
3, All
GroupChildMapping
GroupId, ChildId
1, 1
2, 1
2, 2
GroupGroupMapping
GroupId, ParentGroupId
1, 3
1, 3
I am looking for query to find indirect parents for LeafChilds.
In above example – child1 is in Group1 and Group1 has parent group as ALL. So indirectly child1 is in ALL group as well.
OUTPUT
Child1, Group1
Child1, ALL
Best Answer
I believe the below will work for you. I made the final result return ID's rather than names so that you could see how to grab additional attributes from the base tables if necessary.
I also just realized that this only handles one level of group to parent, would need to slide in a recursive CTE after CTE_RawData to handle that scenario. Is that something your query needs to do? Or is one level of parent group enough?