I try to aggregate values per group into one XML. I am on SQL Server 2016.
My raw-data looks like this (here strictly stiped down to the problem. My real values are not generic.):
create table #data (group_id int, value1 char(10), value2 char(10), value3 char(10));
insert #data values
(1,'a1', 'b1', 'c1'),
(1,'a2', 'b2', 'c2'),
(1,'a3', 'b3', 'c3'),
(2,'a4', 'b4', 'c4');
I am looking for an aggregate function that would return one XML (per group)
select
group_id,
**my_XML_aggregation** as [XML-values]
from #data
group by group_id
The expected result XML for the first group (group_id = 1) should look like (names of the elements are not relevant to the problem)
<group>
<row>
<value1>a1<value1>
<value2>b1<value1>
<value3>c1<value1>
</row>
<row>
<value1>a2<value1>
<value2>b2<value1>
<value3>c2<value1>
</row>
<row>
<value1>a3<value1>
<value2>b3<value1>
<value3>c3<value1>
</row>
</group>
I know how to aggregate a pattern-separated string. This would not do the job. Even to put the pattern-aggregated string into one XML element is no alternative. I am looking for the structured information within the XML.
Try like this:
outputs