I am currently working in SQL server 2005 and table contain million of rows. The table have following rows and columns
ID PO_ID Event_ID Item_ID
1 22 970 123456
1 22 970 123457
1 23 970 1234589
1 22 971 12345790
1 22 971 12345792
I want to concat column item_ID for multiple column group "ID, PO_ID, Event_ID" The output Will be like this
ID PO_ID Event_ID Item_ID
1 22 970 123456,123457
1 23 970 1234589
1 22 971 12345790,12345792
I have the following SQL query
select ID, PO_ID, Event_ID,
substring(
( SELECT ','+ Item_ID)
FROM table as a
WHERE a.ID=table.ID
AND a.PO_ID=table.PO_ID
and a.event_ID=table.event_ID
FOR XML PATH ('')
)
from table
group by ID,PO_ID,Event_ID;
But this query is really slow in terms of performance Is there any optimized way to do this in SQL server 2005? Any help will be appreciated.
Note : I don't have permissions to create UDF or indexes.