I'm finding a way to aggregate strings from different rows into a single row. I'm looking to do this in many different places, so having a function to facilitate this would be nice. I've tried solutions using COALESCE
and FOR XML
, but they just don't cut it for me.
String aggregation would do something like this:
id | Name Result: id | Names
-- - ---- -- - -----
1 | Matt 1 | Matt, Rocks
1 | Rocks 2 | Stylus
2 | Stylus
I've taken a look at CLR-defined aggregate functions as a replacement for COALESCE
and FOR XML
, but apparently SQL Azure does not support CLR-defined stuff, which is a pain for me because I know being able to use it would solve a whole lot of problems for me.
Is there any possible workaround, or similarly optimal method (which might not be as optimal as CLR, but hey I'll take what I can get) that I can use to aggregate my stuff?
for xml
not work for you?for xml
shows a 25% usage in terms of query performance (a bulk of the query!)for xml path
query. Some faster than others. It could depend on your data but the ones usingdistinct
is in my experience slower than usinggroup by
. And if you are using.value('.', nvarchar(max))
to get the concatenated values you should change that to.value('./text()[1]', nvarchar(max))