This is a similar problem I had recently trying to pivot a two column table; MS Access convert and summarise two rows into columns with unique names
'Name' is a reserved word and 'ID' is usually a auto-index with unique numbers so I changed your columns to UserID and UserName respectively.
There are some problems with creating the answer in a single subquery so I ended up doing this:
- Create a temporary table with an index:
SELECT t1.UserID, t1.UserName,
(SELECT COUNT(*) + 1
FROM Table1 t2
WHERE t1.UserID = t2.UserID and t2.UserName < t1.UserName) AS [Index]
INTO Table1_indexed
FROM Table1 AS t1;
- create a temporary cross tab table:
TRANSFORM First(Table1_indexed.UserName) AS FirstOfUserName
SELECT Table1_indexed.UserID FROM Table1_indexed
GROUP BY Table1_indexed.UserID
PIVOT Table1_indexed.Index;
- concatenate the name fields
SELECT Table1_crosstab.UserID, Table1_crosstab.[1], Table1_crosstab.[2],
IIf([1] Is Not Null,[1]) & IIf([2] Is Not Null,", " & [2]) AS ConcatenatedName
FROM Table1_crosstab;
If you have more than two name fields you could adjust the concatenate query to the maximum number you expect.
It might be possible to merge these steps into a single query but I've not yet found a way.