I have written the following SQL query:
SELECT DISTINCT i.IncidentReference Reference, s.Name Site, it.Description IncidentTypes
, i.Description, FORMAT(i.CreatedOn,'d MMM yyyy', 'en-US' ) Logged
FROM Incident i
INNER JOIN TEST_USWM_TestAutomation.TEST_USWM.uvw_AvailaibleSites s ON i.SiteId = s.Id
JOIN IncidentIncidentType iit ON i.Id = iit.IncidentId
JOIN IncidentType it ON iit.IncidentTypeId = it.Id
JOIN IncidentStatus ins ON i.IncidentStatusId = ins.Id
WHERE s.Id = 1 /*:siteId */
AND ins.Name = 'Logged'
AND i.CreatedOn BETWEEN DATEADD(DAY, -30, GETUTCDATE()) AND GETUTCDATE()
Which returns the following results:
Reference Site IncidentTypes Description Logged
7032 0110 Refrigeration Outage njfdgdf 25 Sep 2019
7095 0110 Fire/False Evacuation testing 2 Oct 2019
7096 0110 Fire/False Evacuation testing 2 Oct 2019
7097 0110 Fire/False Evacuation test 2 Oct 2019
7097 0110 Flood test 2 Oct 2019
7097 0110 Gas Leak/Suspected Gas Leak test 2 Oct 2019
7098 0110 Flood testing 2 Oct 2019
7168 0110 Fire/False Evacuation test 7 Oct 2019
7330 0110 Refrigeration Outage Test 15 Oct 2019
However, I am looking to combine all rows with the same Reference into one row. So for example, for Reference - 7097, I would want to see the column IncidentTypes look like - Gas Leak/Suspected Gas Leak, Fire/False Evacuation, Flood.
What would be the best way to do this?
EDIT: I have tried the query that @GMB posted below, but I am getting the error - The function 'STRING_AGG' may not have a WITHIN GROUP clause. This seems to be an issue with the version of SQL Server that I am using. Is there another way to get around this issue without upgrading the version ?
EDIT: Output for @Gordon Linoff's query -

JOINis the same asINNER JOIN?groub byclause and appropriate aggregate functions for the columns (e.g. concatenate strings). What exactly you could use would depend on the database you're using though, because they all support different aggregate functions (there's a common set but most have their own additions).listagg()is part of the SQL standard.