Reputation: 59
STUFF((SELECT distinct ',' + QUOTENAME(c.Error_Code)
FROM
(SELECT Connection_type, Error_Code, Count
FROM
(SELECT
Connection_Type, error_code, count(*) AS count,
row_number() over(partition by Connection_Type order by count(*) desc) as ROWNUM
FROM
Staging
WHERE
TransactionDate >= convert(varchar, getdate() -1, 111)
AND Status != 'Deliver'
GROUP BY
Connection_Type, error_code) a
WHERE
rownum <= 10) c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
What does the line
.value('.', 'NVARCHAR(MAX)'
mean?
Upvotes: 1
Views: 4080
Reputation: 116
The value method converts the XML data to NVARCHAR, because the STUFF function (it removes the first comma from the list of values) expect character expression as first argument.
You can try this here: SQL Fiddle
If you remove the value, you got an error.
Upvotes: 1