Say we have two tables in an MS Access db:
Service Users:
| ID | Name | Other details... |
| 1 | Joe | Blah... |
| 2 | Fred | Qwerty... |
| 3 | Bob | Something else...|
Teams providing services:
| ID | TeamID | UserID |
| 1 | T1 | 1 |
| 2 | T2 | 1 |
| 3 | T2 | 2 |
| 4 | T3 | 2 |
| 5 | T3 | 3 |
I need to produce a summary query that produces a single row for each user, with the first several teams (by TeamID) assigned sitting in separate columns. Like:
Query:
| UserID | Name | Team1 | Team2 |
| 1 | Joe | T1 | T2 |
| 2 | Fred | T2 | T3 |
| 3 | Bob | T3 | Null |
I can get the Team1 column using max() from a sub select query, but I'm having a complete mental block on how to achieve Team2, Team3, etc. (Yes, I know that if there are more teams assigned to a user than I create columns the query will lose that information: that isn't a concern).
Edit: To clarify, the number of columns in the query will be fixed (in the actual query, there will always be 7). If there are less teams than columns the additional columns should be Null (as in example). If there are more teams than columns, only the first 7 teams will be shown in this summary.
Edit 2 - Possible solution which doesn't work...:
I tried...
SELECT UserTable.ID As UID, UserTable.Name,
(SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID
ORDER BY TeamID) As Team1
FROM UserTable
... which works fine. Unfortunately...
SELECT UserTable.ID As UID, UserTable.Name,
(SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID
ORDER BY TeamID) As Team1,
(SELECT TOP 1 TeamID FROM TeamTable WHERE UserTable.ID = TeamTable.UserID
AND TeamID <> Team1 ORDER BY TeamID) As Team2
FROM UserTable
... throws up a parameter box for Team1. An ideas on how to skip the first/second/etc... values from an jet query?