Concatenating multiple row data into a single row in SQL Server.
In one of my recent projects, I needed to combine multiple row data values into a single one-liner. No idea about performance, but I leveraged the FOR XML PATH for my example.. Thanks to this link for the tip.
USE AdventureWorks
GO
SELECT
CustomerID,
SalesOrderIDs = REPLACE(
(
SELECT
SalesOrderID AS [data()]
FROM
Sales.SalesOrderHeader soh
WHERE
soh.CustomerID = c.CustomerID
ORDER BY
SalesOrderID
FOR XML PATH ('')
), ' ', ',')
FROM
Sales.Customer c
ORDER BY
CustomerID

0 Comments