Let's say we have two tables in 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 create a composite query that creates one row for each user, with the first few teams (by TeamID) assigning a seat in separate columns. How:
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 the subselect request, but I have a full mental block on how to reach Team2, Team3, etc. (Yes, I know that if more commands are assigned to a user than I create columns, the query will lose this information: this is not a problem).
: , ( 7). , , Null ( ). , , 7 .
2 - , ...:
...
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
... . ,...
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
... Team1. , // .. ?