Reputation: 1256
SELECT Name, Members
FROM People
WHERE Name LIKE '%Kako%'
GROUP BY Name, Members
I am getting this result from the SQL query shown above:
Name | Members
------------+-------------
James Kako | Rangers
James Kako | Wind
James Kako | Poers
James Kako | Lohe
James Kako | Jubo
J. Kako | Wind
J. Kako | Lohe
J. Kako | Poers
I would like to merge Members
column like this:
Name | Members
------------+---------------------------------
James Kako | Rangers, Wind, Poers, Lohe, Jubo
J. Kako | Wind, Lohe, Poers
Thank you in advance
Upvotes: 0
Views: 61
Reputation: 785
This solution works as well:
DECLARE @char VARCHAR(MAX);
SELECT @char = COALESCE(@char + ', ' + members, members)
FROM MergeColumn ;
Select name , @char as Mergerd from MergeColumn group by name;
Also, you can take a look at this link for more solutions to the question you asked: How to concatenate text from multiple rows into a single text string in SQL server?
Upvotes: 0
Reputation: 65363
Using STRING_AGG
is the direct way for this type of problems :
SELECT Name, STRING_AGG(Members, ', ') as Members
FROM People
WHERE Name LIKE '%Kako%'
GROUP BY Name
ORDER BY Name DESC;
Upvotes: 1