Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SQL select statement to combine multiple row vales into one field
SELECT p.Name, substring
((SELECT ', ' + k.Number FROM dbo.Keys k
JOIN dbo.Keys_Assignments ka ON ka.KeyID = k.KeyID
WHERE ka.PersonID = p.PersonID
FOR XML PATH('')), 2, 1000) [Keys]
FROM dbo.People p
SELECT k.Number, SUBSTRING
((SELECT '; ' + p.Name FROM dbo.People p
JOIN dbo.Keys_Assignments ka ON ka.PersonID = p.PersonID
WHERE ka.KeyID = k.KeyID
FOR XML PATH('')), 2, 1000) [Names]
FROM dbo.Keys k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment