Skip to content
Advertisement

How to combine multiple rows (one column) into One row

I am using SQL server 2017 and I have a scenario where I want to combine multiple rows of data into one row. Please see the below screenshot and let me know if that is possible. Thank you. Apologies if anything is missing.

Please let me know the query on how to achieve this

Please see the below pic for the expected output I would like to see

Advertisement

Answer

SQL Server has supported string_agg() since 2017:

select employeeid, lastname,
       string_agg(projectname, ',') within group (order by projectname) as projectnames
from t
group by employeeid, lastname;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement