请帮我解决这个错误。
SELECT StateProvince,STRING_AGG(AddressID, ',') WITHIN GROUP (ORDER BY AddressID)
FROM [SalesLT].[Address] GROUP BY StateProvince;
我找不到这个错误,但它说
'(' 附近的语法不正确。
请帮我解决这个错误。
SELECT StateProvince,STRING_AGG(AddressID, ',') WITHIN GROUP (ORDER BY AddressID)
FROM [SalesLT].[Address] GROUP BY StateProvince;
我找不到这个错误,但它说
'(' 附近的语法不正确。
将数据库从 SQL 2008R2 升级到 2017 后出现同样的问题。通过将 Compatability Level 设置为 110(或更高?)来修复
对于 SQL 服务器 2017
SELECT StateProvince,
STRING_AGG(AddressID, ',') WITHIN GROUP (ORDER BY AddressID) AS AddressID
FROM [SalesLT].[Address] GROUP BY StateProvince;
演示
http://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=69e5f9e9c5f3cab62e4b2eb9fb678299
对于 SQL SERVER 低于 2017
SELECT
StateProvince,
AddressID = STUFF((
SELECT ',' + CAST(md.AddressID AS NVARCHAR)
FROM [SalesLT].[Address] md
WHERE m.StateProvince = md.StateProvince
ORDER BY AddressID
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM [SalesLT].[Address] m
Group by StateProvince
演示
http://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=a1722450c70c946e9b53ae23785f4919
我看不到与您收到的消息相关的错误。对于大数据,它会抱怨 8000 字节的限制。这在 MS SQL 2017、AdventureWorks 示例数据库(不是数据较少的精简版)下运行良好:
SELECT StateProvinceID,
STRING_AGG(cast(AddressID as varchar(MAX)), ',') WITHIN GROUP (ORDER BY AddressID) as AdressIDS
FROM [Person].[Address]
GROUP BY StateProvinceID;