• First of all that's amazing, it give exactly the required output. Thank you very much. Currently I'm really struggling to see how it works but I'll try and disect this evening.

    There's a small problem though, if I run this on my real data, it runs out of memory after 30 minutes.

    There are 474 users who belong to on average 6 groups (max 32).

    The average length of a group name is 20 characters (max 47).

    In the interests of being able to get the task nailed today (the business are badgering me) is there something we can do to optimise?

    I've created a table of the 88 distinct groups with a int column 1-88 indexing them.

    Every row in the user table now has an extra int column containing this GrpID.

    Is that helpful and how would it change the query pls?