I have a query in SQL Server 2014 that returns a list of students that belong to more than one club. This data is retrieved from a relationship table listed below:
ID Student ClubName
==============================
1 Bob Chess
2 Bob Tennis
3 Frank Soccer
4 Frank Math
5 Tom Chess
6 Tom Math
7 Tom Drama
I can list the number students that belong to more than one club using the following query.
SELECT Student, COUNT(ClubName)
FROM StudentClub
GROUP BY Student
Having COUNT(ClubName) > 1
Student Number of Clubs
=========================
Bob 2
Frank 2
Tom 3
What I would like to do is list the Club Names each student belongs to in columns, like this ...
Student Club1 Club2 Club3
=====================================
Bob Chess Tennis
Frank Soccer Math
Tom Chess Math Drama
Is this possible to do in the same query that determines which students belong to more than one club?
P.S. I gave the example using names rather than IDs as I felt it would be easier to explain/read. In my real table I use Student IDs and Club IDs.