日期:2014-05-17 浏览次数:20504 次
create table 表A
(id int, name varchar(5), [rule] xml)
insert into 表A(id,name,[rule])
select 1, 'ddd',
'<legendRule>
<questionRule>
<questionCondition id="Q1">
<answer>A</answer>
<answer>B</answer>
</questionCondition>
<questionCondition id="Q2">
<answer>C</answer>
<answer>D</answer>
</questionCondition>
<questionCondition id="Q3">
<answer>B</answer>
<answer>C</answer>
</questionCondition>
<questionCondition id="Q4">
<answer>A</answer>
<answer>C</answer>
</questionCondition>
</questionRule>
</legendRule>'
with t1 as
(select a.id,
o.value('../@id','varchar(5)') 'ids',
o.value('.','varchar(5)') 'ans'
from 表A a
cross apply [rule].nodes('/legendRule/questionRule/questionCondition/answer') x(o)
),t2 as
(select a.id,
a.ids+':'+stuff((select '、'+b.ans from t1 b
where b.id=a.id and a.ids=b.ids
for xml path('')),1,1,'') 'ids2'
from t1 a
group by a.id,a.ids
),t3 as
(select a.id,
stuff((select ';'+b.ids2 from t2&n