I've seen people combining both FOR XML
and STUFF
statements to achieve a STRAGG
-like in Sql Server.
I have a problem with this approach in that, given I have a couple of fields I want to aggregate into strings to the outermost query, I am repeating the same select statement over and over again, for each new aggregation.
As far as I understand, however, this FOR XML
+ STUFF
solution has to be applied in the innermost level - meaning I can't add an inline view with the select statement I will be using in the aggregations and apply Distinct
on it, because I will already have joined the results with each of the distinct values.
In a short, adding to the example taken from the site cited above, here's what I want to do:
http://www.sqlfiddle.com/#!3/84199/2/0
Is there any better solution when you want to do that for a number of aggregations, so to avoid such redundant performance penalties ?