CREATE FUNCTION dbo.SplitString
( @List nvarchar(max), @Delim nvarchar(255)
)
RETURNS TABLE WITH SCHEMABINDING
AS
RETURN ( WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1
FROM n WHERE n <= LEN(@List))
SELECT [Value] = SUBSTRING(@List, n,
CHARINDEX(@Delim, @List + @Delim, n) - n)
FROM n WHERE n <= LEN(@List)
AND SUBSTRING(@Delim + @List, n, DATALENGTH(@Delim)/2) = @Delim
);
GO
Query
Summary
Select v.Product, x.Vendors
from #Vendor v
join (Select Product,
STUFF(
( Select distinct '|' + ltrim(rtrim([value]))
from #Vendor v1
cross apply dbo.SplitString(VendorID, '|')
where v1.Product = v.Product
FOR XML PATH('')
),
1,
1,''
) AS Vendors
FROM #Vendor V
GROUP BY Product) x
on x.Product = v.Product
This table data is for reporting, then found the issue of vendorID not correct order is causing issue. so we wanted everything to same so it all be grouped together.
You can certainly use what you want but that split function will make sucking sounds for performance. I recommend you scroll up for the link to the DelimitedSplit8K function,