I have a query that I normally group_concat. I need to know if dbo.group_concat has the ability to pair the result of a query into pairs of 3 (as an example).
For example:
select size, pattern, dbo.group_concat(mass) mass
from labels
group by size, pattern
Result is
Size Pattern Mass
-----------------------------------------------------------
234 ZYL 22.43,55.32,33.24,22.53,56.32,40.32,50.21,32.21
234 ZA 50.00,56.23,21.23,50.21
I'd like to have this result
Size Pattern Mass
--------------------------------
234 ZYL 22.43,55.32,33.24
234 ZYL 22.53,56.32,40.32
234 ZYL 50.21,32.21
234 ZA 50.00,56.23,21.23
234 ZA 50.21
This can be done easily, if we have a
group IDcolumn. This column will group rows per three, sorting the data by[mass]value. Since, we have not got such column, we need to calculated it. The steps as are as following:ROW_NUMBERfunction to sort the rows and know which three consequence rows form a groupsize, patternentity and then join the next row ... then join the next, until we get them allThis is full working example of the above:
Now, in the final
SELECTwe just need to add your initial code, but grouping by the[group]column also:In my system, the above yields this:
Some considerations:
massvalue (and in your example, you are not) - this is completely up to you; you can change the ordering to whatever you need, even usingSELECT 1in theORDER BYclause of theROW_NUMBERfunction to get some random ordering;concatenateSQL CLR function on my systems allows setting order; in my case, I have concatenated the rows using theRowIDvalue; if your function does not allow specifying such order you rely on the .net code behind it, so you can get different order of the values in the final CSV list.