感谢@Isaac提供的数据脚本!
for <List> elements in <Branch> node, like <Branch><List/><List/></Branch>:
CREATE TABLE #mytable
(
Action VARCHAR(10),
Action2 VARCHAR(10),
Name VARCHAR(50),
Action3 VARCHAR(10),
Batch INT
);
INSERT INTO #mytable(Action,Action2,Name,Action3,Batch)
VALUES
('add','PL','Steve','add',1),
('add','PL','Steve','add',3),
('add','PL','Steve','add',4),
('add','PL','Steve','add',5),
('add','PL','Steve','add',1),
('add','PL','Steve','add',3),
('add','PL','Steve','add',4),
('add','PL','Steve','add',5);
INSERT INTO #mytable(Action,Action2,Name,Action3,Batch)
VALUES
('update','PL','John','insert',5),
('update','PL','Paul','insert',1),
('update','PL','Chris','delete',3),
('update','PL','Mary','update',4),
('update','PL','Jane','delete',5);
select a1.Action as '@Action', s.brancexml as '*'
from
(
select distinct Action
from #mytable
) as a1
cross apply
(
select
(
select a2.Action2 AS '@Action', a2.Name as '@Name', x.listxml as '*'
from
(
select distinct Action2, Name
from #mytable AS b
where b.Action = a1.Action
) AS a2
cross apply
(
select
(
select distinct c.Action3 as '@Action', c.Batch as '@Batch'
from #mytable AS c
where c.Action = a1.Action AND c.Action2 = a2.Action2 AND c.Name = a2.Name
for xml path('List'), type
) AS listxml
) as x
for xml path('Brance'), type
) as brancexml
) as s
for xml path('Start'), root('Entries'), type
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。