Skip to content
Advertisement

Check if a pair of records belong to multiple group IDs

I have a table that contains 2 IDs – UserID and GroupID. I need to pull a list of all UserIDs that “share” the same GroupID at least 4 times

So, based on the following data set:

I’m trying to generate the following result:

UserID A UserID B NumberOfOccurrences
1 2 4
2 3 4
1 3 5

I’ve created an SQLFiddle for it. I’ve tried to achieve this via JOINs and sub-queries, but I’m not entirely sure how to properly proceed with something like this.

Advertisement

Answer

Do a self join. GROUP BY. Use HAVING to make sure at least 4 common GroupID’s.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement