I have a code snippet that works fine in MS SQL 2019, but it doesn't work in MS SQL 2016. I know the problem is caused by the STRING_AGG
function. When I searched the web, I saw solutions for a single column. But this seems a little more complicated. So, How do I convert a query containing more than one STRING_AGG
to FOR XML PATH
?
Here is the code snippet:
SELECT DISTINCT
STRING_AGG(rvsm.Plate, ',') WITHIN GROUP (ORDER BY rvsm.Plate),
MIN(rvsm.Date),
MIN(rvsm.StopId),
rvsm.RouteVariantId,
MIN(rvsm.ValidatorNo),
MIN(rvsm.Id),
COUNT(*),
STRING_AGG(rvsm.ValidatorNo, ',')
FROM
VehicleTrackingLastLoc as rvsm
INNER JOIN RouteVariantStops rvs ON rvsm.RouteVariantId = rvs.RouteVariantId AND rvsm.StopId = rvs.StopsId
INNER JOIN RouteVariant rt ON rvsm.RouteVariantId = rt.Id
INNER JOIN Stops st ON rvsm.StopId = st.Id
WHERE
Contact = 'ON' AND Date > dateadd(second, -30, GetDate())
GROUP BY
rvs.RowNo,
rvsm.RouteVariantId
HAVING
COUNT(*) > 1