我有下表:tblFile
我想要的输出:
我将多行连接成一个文本字符串;但是,我无法正确分组。现在代码只显示FileNameString字段中的每条记录:AAA,BBB,CCC,DDD,EEE,FFF
任何有关分组的建议!
SELECT FileID, Stuff(
(SELECT N', ' + CONVERT(Varchar, FileName)
FROM tblFile FOR XML PATH(''),TYPE )
.value('text()[1]','nvarchar(max)'),1,2,N'')AS FileNameString
From tblFile
GROUP BY FileID
答案 0 :(得分:17)
试试这个 -
SELECT DISTINCT
fileid
, STUFF((
SELECT N', ' + CAST([filename] AS VARCHAR(255))
FROM tblFile f2
WHERE f1.fileid = f2.fileid ---- string with grouping by fileid
FOR XML PATH (''), TYPE), 1, 2, '') AS FileNameString
FROM tblFile f1