I am using SQL Server 2008.
I have a table of items and the categories they belong to.
-----------------------------------------------------------------
ItemId | Category1 | Category2 | Category3 | Category4
-----------------------------------------------------------------
1 | Val1, Val2 | Val3, Val4 | Val5, Val6 | Val7, Val8
1 | Val9 | | |
1 | Val10 | Val11 | |
*Edit1
There is no index on the table and ItemId is not the primary key. Each item might belong to more than one category.
I want to Select distinct ItemId from the table and have all possible Categories as comma separated for each column.
Sample Output -
------------------------------------------------------------------------------
ItemId | Category1 | Category2 | Category3 | Category4
------------------------------------------------------------------------------
1 | Val1, Val2, Val9, Val10 | Val3, Val4, Val11 | Val5, Val6 | Val7, Val8
I am able to achieve this using STUFF and FOR XML PATH using the below query. However, I had a look at the execution plan and it seemed like probably not the best way to do it. I am using 4 different SELECT statements for each of the categoryies. Can it all be merged into one? I am looking for the most efficient query to achieve the same result as the below query.
SELECT DISTINCT
t1.tblItemId,
STUFF((
SELECT DISTINCT ',' + t2.Category1
FROM tblCategory t2
WHERE t1.tblItemId = t2.tblItemId
FOR XML PATH ('')), 1, 1, '') AS Category1,
STUFF((
SELECT DISTINCT ',' + t2.Category2
FROM tblCategory t2
WHERE t1.tblItemId = t2.tblItemId
FOR XML PATH ('')), 1, 1, '') AS Category2,
STUFF((
SELECT DISTINCT ',' + t2.Category3
FROM tblCategory t2
WHERE t1.tblItemId = t2.tblItemId
FOR XML PATH ('')), 1, 1, '') AS Category3,
STUFF((
SELECT DISTINCT ',' + t2.Category4
FROM tblCategory t2
WHERE t1.tblItemId = t2.tblItemId
FOR XML PATH ('')), 1, 1, '') AS Category4
FROM tblCategory t1
where t1.tblItemId IS NOT NULL
*Edit1 - Currently multiple values don't exist in each row delimited by commas. I have used them as it might be a possibility in the future and no one is restricting it. I am using a Common Table Expression to create a temp table with the desired output that can be used for my front end.