如何将一个字段的所有记录放入一个字段
Id, No , FDevice
1 , 1 , 'A'
2 , 1 , 'B'
3 , 1 , 'C'
4 , 2 , 'D'
5 , 2 , 'E'
我想要
No , FDevice
1 , A-B-C
2 , D-E
感谢您的帮助
答案 0 :(得分:2)
使用STUFF()
- 将字符串插入另一个字符串。
SELECT
[No],
STUFF(
(SELECT '-' + [FDevice]
FROM TableName
WHERE [No] = a.[No]
FOR XML PATH (''))
, 1, 1, '') AS FDevice
FROM TableName AS a
GROUP BY [No]
答案 1 :(得分:1)
使用select ... for xml path('')
在SQL Server中有一个众所周知的聚合连接解决方案,但我不得不说许多人使用它的方法不正确。正确的方法是
select
a.[No],
stuff(
(
select '-' + t.[FDevice]
from TableName as t
where t.[No] = a.[No]
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 1, '') as FDevice
from (select distinct [No] from TableName) as a;
<强> sql fiddle demo 强>
主要部分是在查询中使用xml类型,然后使用value
函数将其转换为varchar,否则最终会导致错误转换的特殊字符,如'>', '<', '&'
等。 SQLfiddle以某种方式没有显示差异,但是这里有一个脚本可以告诉你如果你不使用xml类型会发生什么:
declare @TableName table
([Id] int, [No] int, [FDevice] varchar(3))
;
INSERT INTO @TableName
([Id], [No], [FDevice])
VALUES
(1, 1, 'A<'),
(2, 1, 'B'),
(3, 1, '&C'),
(4, 2, 'D'),
(5, 2, 'E')
;
SELECT
[No],
STUFF(
(SELECT '-' + [FDevice]
FROM @TableName
WHERE [No] = a.[No]
FOR XML PATH (''))
, 1, 1, '') AS FDevice
FROM @TableName AS a
GROUP BY [No];
outputs
No FDevice
--------------------
1 A<-B-&C
2 D-E
select
a.[No],
stuff(
(
select '-' + t.[FDevice]
from @TableName as t
where t.[No] = a.[No]
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 1, '') as FDevice
from (select distinct [No] from @TableName) as a;
outputs
No FDevice
--------------------
1 A<-B-&C
2 D-E