Thank you for this tip with the STUFF function.
Is the order by in the subquery really needed? I think the for xml does it anyways. If you don't use the order by; you may use distinct to get every value only once.
WITH CTE AS
(
SELECT DISTINCT
AccountNumber
FROM #TestData
)
SELECT AccountNumber,
CommaList = STUFF((
SELECT distinct ',' + Value --<<-- to get every value only once
FROM #TestData
WHERE AccountNumber = CTE.AccountNumber
--ORDER BY Value --<<--
FOR XML PATH(''),
TYPE).value('.','varchar(max)'),1,1,'')
FROM CTE
ORDER BY AccountNumber;
greetings Reto E.