问题
I want to convert the following MYSQL query to MS SQL Server. But, I am sure that GROUP_CONCAT
doesn't exist in MS SQL Server prior to 2017. There is a function in SQL SERVER 2017. Can anyone help me?
SELECT region, GROUP_CONCAT(route_name) AS route_name
FROM route_details
LEFT JOIN ride_details ON route_id = r_id
WHERE region != '' AND service_date = '2019-09-02'
GROUP BY region
回答1:
You want string_agg(). The syntax is a bit different (typically, the separator is mandatory, while it defaults to ,
in MySQL, and SQLServer wants within group
for the order by
):
SELECT region, STRING_AGG(route_name, ',') AS route_name
FROM route_details
LEFT JOIN ride_details ON route_id = r_id
WHERE region != '' AND service_date = '2019-09-02'
GROUP BY region
来源:https://stackoverflow.com/questions/58838868/corresponding-group-concat-in-sql-server-2017