DECLARE @combinedString VARCHAR(MAX)
SELECT @combinedString = COALESCE(@combinedString + ', ', '') + DriversTransportDetails.DriverName from DriversTransportDetails
select trID, @combinedString as C
from DriversTransportDetails
group by DriversTransportDetails.trID
这是我的代码,我有一个名为DriversTransportDetails
的表格,其中包含trID
列int
}和driverName
(nvarchar
)
trID
不是唯一的,因此trID
可以在多行中取决于驱动程序,例如:
TrID DriverName
1 Tony
1 Rony
2 Jeorge
3 Jim
我想将COALESCE
驱动程序名称行放到一行中,所需的结果是:
TrID C
1 Tony, Rony
2 Jeorge
3 Jim
问题是结果不正确,它显示所有驱动程序合并到列c中,如下所示:
TrID C
1 Tony, Rony, Jeorge, Jim
2 Tony, Rony, Jeorge, Jim
3 Tony, Rony, Jeorge, Jim
有什么问题?
非常感谢:)