I work with a goups table.
GroupMembers (GroupName, MemberName)
There is a line for each member of the group, and the group may contain other groups and users.
I would like to extract a list of pairs of GroupName, MemberName, where MemberName is just a list of users. Essentially tree alignment. I did something similar to this and manually wrote queries that exported each level into a separate table and then consolidated it as soon as I reached the last level.
The tree seems unbalanced and does not have a fixed number of levels. I looked at examples of recursive queries and did not have much luck in their implementation.
Does anyone have any good recommendations where I can go to put a togeather on this elegant solution?
Many thanks!
ps If this helps, I am working with SQL Server 2008.
UPDATE: I came across a recursive CTE. My only problem is that the data has circular references: (.
This code that I used for the request:
WITH Members AS ( --Init SELECT GroupName, MemberName FROM GroupMembers WHERE MemberName NOT IN (Select GroupName from GroupMembers) UNION ALL --Recursive Exe SELECT h.GroupName, h.MemberName FROM GroupMembers h INNER JOIN Members m ON h.MemberName = m.GroupName ) Select * into GroupMembersFlattened from Members OPTION (MAXRECURSION 1500)
Is there a way to exclude circular references / clense data before executing the above query?
Thanks!
Example Circular / Circular Reference An example of a circular reference is where the data contains the following: -
GroupMember, MemberName Group1, Group2 Group1, User1 Group2, Group3 Group2, User2 Group3, Group1
Thanks for the tip of Mikael!