I've got this SELECT :
SELECT OS.returnmessageid,
OS.tagconfig,
CASE
WHEN OP.messagetype = 202
AND OS.datapointid IN ( 1, 3 ) THEN
CONVERT(NVARCHAR(max), OS.lookupvalue)
ELSE Try_convert(Nvarchar(max), OS.value)
END AS Value,
OS.unitnumber,
OD.messageutc,
OP.messagetype,
OS.isproccessed,
OS.id,
Row_number()
OVER (
partition BY OS.returnmessageid, OS.unitnumber, OD.messageutc,
OP.messagetype
ORDER BY OS.returnmessageid, OS.unitnumber, OD.messageutc,
OP.messagetype )
AS RowNum
FROM ooocommreturnmessagesscaling OS
LEFT JOIN ooocommreturnmessagesparsing OP
ON OP.id = OS.returnmessageid
LEFT JOIN ooocommreturnmessagedetails OD
ON OP.returnmessageid = OD.id
Example result set:
I'd like to add a column that has all the associated values for OS.Id for the same combination of OS.ReturnMessageId, OS.UnitNumber, OD.MessageUTC, OP.MessageType .
This is the output that I'm looking for:
As you can see that last field IdAggregate has been added as a column separated list of OS.Id for the same combination of OS.ReturnMessageId, OS.UnitNumber, OD.MessageUTC, OP.MessageType
How do I add this IdAggregate column?


STRING_AGG()as inSTRING_AGG(OS.id, ',') OVER(PARTITION BY OS.returnmessageid) WITHIN GROUP(ORDER BY OS.id) AS IdAggregate. Unfortunately, this is not supported and results in the message "The function 'STRING_AGG' is not a valid windowing function". (Why?)Try_convert()inTry_convert(Nvarchar(max), OS.value)? No harm done, but I don't think that would ever fail. (Even a nullOS.valueinput would quietly convert to null.)