TSQL:Return 个组的元素为 XML
TSQL: Return elements of a group as XML
我尝试将每组的值聚合为一个 XML。我在 SQL Server 2016.
我的原始数据看起来像这样(这里严格限制了问题。我的真实值不是通用的。):
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');
我正在寻找一个 return 一个 XML (每组)
的聚合函数
select
group_id,
**my_XML_aggregation** as [XML-values]
from #data
group by group_id
第一组 (group_id = 1) 的预期结果 XML 应该如下所示(元素名称与问题无关)
<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>
我知道如何聚合模式分隔的字符串。这不会完成这项工作。甚至将模式聚合字符串放入一个 XML 元素中也是别无选择。我正在寻找 XML.
中的结构化信息
这样试试:
drop table if exists #data ;
create table #data (group_id int, value1 varchar(10), value2 varchar(10), value3 varchar(10));
insert #data values
(1,'a1', 'b1', 'c1'),
(1,'a2', 'b2', 'c2'),
(1,'a3', 'b3', 'c3'),
(2,'a4', 'b4', 'c4');
with groups as
(
select distinct group_id from #data
)
select group_id,
(
select value1, value2, value3
from #data
where group_id = groups.group_id
for xml path, root('group'), type
) group_doc
from groups
产出
group_id group_doc
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 <group><row><value1>a1</value1><value2>b1</value2><value3>c1</value3></row><row><value1>a2</value1><value2>b2</value2><value3>c2</value3></row><row><value1>a3</value1><value2>b3</value2><value3>c3</value3></row></group>
2 <group><row><value1>a4</value1><value2>b4</value2><value3>c4</value3></row></group>
我尝试将每组的值聚合为一个 XML。我在 SQL Server 2016.
我的原始数据看起来像这样(这里严格限制了问题。我的真实值不是通用的。):
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');
我正在寻找一个 return 一个 XML (每组)
的聚合函数select
group_id,
**my_XML_aggregation** as [XML-values]
from #data
group by group_id
第一组 (group_id = 1) 的预期结果 XML 应该如下所示(元素名称与问题无关)
<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>
我知道如何聚合模式分隔的字符串。这不会完成这项工作。甚至将模式聚合字符串放入一个 XML 元素中也是别无选择。我正在寻找 XML.
中的结构化信息这样试试:
drop table if exists #data ;
create table #data (group_id int, value1 varchar(10), value2 varchar(10), value3 varchar(10));
insert #data values
(1,'a1', 'b1', 'c1'),
(1,'a2', 'b2', 'c2'),
(1,'a3', 'b3', 'c3'),
(2,'a4', 'b4', 'c4');
with groups as
(
select distinct group_id from #data
)
select group_id,
(
select value1, value2, value3
from #data
where group_id = groups.group_id
for xml path, root('group'), type
) group_doc
from groups
产出
group_id group_doc
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 <group><row><value1>a1</value1><value2>b1</value2><value3>c1</value3></row><row><value1>a2</value1><value2>b2</value2><value3>c2</value3></row><row><value1>a3</value1><value2>b3</value2><value3>c3</value3></row></group>
2 <group><row><value1>a4</value1><value2>b4</value2><value3>c4</value3></row></group>