相当于 WM_CONCAT 函数的 SQL Server
可能的重复:
将字段值连接到 SQL Server 中的字符串
WM_CONCAT 的 SQL Server 等价物是什么?
What is the SQL Server equivalent of WM_CONCAT?
推荐答案
您没有相应的功能,但您仍然可以模拟(使用 CROSS APPLY
和 FORXML PATH('')
).例如,
You don't have an equivalent function for that, but you can still simulate (make useof CROSS APPLY
and FOR XML PATH('')
). example,
USERID ADDRESSLINE1
==========================
1 First Street
1 Second Street
2 32th Street
2 24th Street
2 25th Street
结果
USERID ADDRESSLIST
============================
1 First Street, Second Street
2 32th Street, 24th Street, 25th Street
使用此查询:
SELECT a.UserID,
SUBSTRING(d.Addresses,1, LEN(d.Addresses) - 1) AddressList
FROM
(
SELECT DISTINCT UserID
FROM tableName
) a
CROSS APPLY
(
SELECT [AddressLine1] + ', '
FROM tableName AS B
WHERE A.UserID = B.UserID
FOR XML PATH('')
) D (Addresses)
相关文章