SQL Server SQL Query to group the values based on the key column [duplicate]

qfe3c7zg  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(155)

This question already has answers here:

Comma separated results in SQL (5 answers)
Closed 3 days ago.

I have a table called SOURCE_CATEGORY with SOURCEID and NAME. I have 4020 and 4025, how is it possible to combine the NAME field based on the SOURCEID using SQL Query?

I am hoping to get this result, because I wanted to copy the data in MSSQL to MongoDB which can combine the result into array. I only wanted the NAME field in mongodb. Is it possible to custom the SQL in MSSQL to kind of group them?

zpgglvta

zpgglvta1#

You can do it using string_agg as follows :

select sourceid, string_agg( experience, ',') AS NAME
from source_category
group by sourceid
order by sourceid

相关问题