Here is a sample of my table:


ID Name Codes
1 John D S12, S13, S89
1 John D S98, T12
2 Joe D X27, S28
3 Bob S F12, F13, X43
3 Bob S S44, X38, S90
4 Steve F D25


I want a query that will return the following

ID Name Codes
1 John D S12, S13, S89, S98, T12
2 Joe D X27, S28
3 Bob S F12, F13, X43, S44, X38, S90
4 Steve F D25

Any ideas?

Member Avatar for Rkeast

Use XML PATH('') to concatenate the rows, and STUFF to remove extra ', ' from the beggining of the data like so:

SELECT
  TT.ID,
  TT.Name,
  STUFF((
    SELECT
       ', ' + SQTT.Codes
    FROM @TempThing AS SQTT
    WHERE SQTT.ID = TT.ID
    FOR XML PATH('')
  ), 1, 2, '') AS Codes
FROM @TempThing AS TT
GROUP BY ID, Name

Where @TempThing would obviously be the name of your table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.