我正在尝试获得另一个列,它可以获取其所有计数的id,例如,如果名为total的count列具有2-这些id实际上应该是符合groupby条件的那些>
我尝试使用此代码,但失败
SELECT message,type, count(1) as total,
STUFF((
SELECT ',' + id
FROM dbo.testing md
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM testing
GROUP BY message,type
HAVING count(1) > 1;
我得到的错误:
Msg 156, Level 15, State 1, Line 25
Incorrect syntax near the keyword 'FOR'.
答案 0 :(得分:0)
我想你想要
SELECT message, type, count(1) as total,
STUFF( (SELECT N',' + CONVERT(NVCHAR(MAX), id)
FROM dbo.testing t2
WHERE t2.message = t.message and t2.type = t.type
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
)
FROM testing t
GROUP BY message,type
HAVING count(1) > 1;
这可以解决逻辑问题,但不能解释您遇到的语法错误。