DECLARE @combinedString VARCHAR(MAX)
SELECT @combinedString = COALESCE(@combinedString + ', ', '') + DriversTransportDetails.DriverName from DriversTransportDetails
select trID, @combinedString as C
from DriversTransportDetails
group by DriversTransportDetails.trID
This is my code, I have table called DriversTransportDetails which contains columns trID (int) and driverName (nvarchar)
trID is not unique so trID can be in multiple rows depends on drivers, ex :
TrID DriverName
1 Tony
1 Rony
2 Jeorge
3 Jim
I want to COALESCE driver name rows into one row, desired result :
TrID C
1 Tony, Rony
2 Jeorge
3 Jim
The problem is the result is not correct, it shows all drivers combined into column c, like this :
TrID C
1 Tony, Rony, Jeorge, Jim
2 Tony, Rony, Jeorge, Jim
3 Tony, Rony, Jeorge, Jim
What's the problem ?
Thank you very much :)