Corresponding GROUP_CONCAT() in SQL SERVER 2017 [duplicate]

こ雲淡風輕ζ 提交于 2020-02-06 20:16:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!