日期:2014-05-17 浏览次数:20516 次
create table A表(xmlcontent xml)
insert into A表
select '
<root>
<questions>
<question id="Q1">
<answer>aaa</answer>
<answer>bbb</answer>
</question>
<question id="Q2">
<answer>bbb</answer>
<answer>ddd</answer>
</question>
</questions>
</root>' union all
select '
<root>
<questions>
<question id="Q1">
<answer>aaa</answer>
<answer>ccc</answer>
</question>
<question id="Q2">
<answer>aaa</answer>
<answer>bbb</answer>
</question>
</questions>
</root>'
-- 以字符串形式查询
with t as
(select distinct o.value('.','varchar(10)') 'answer'
from A表
cross apply xmlcontent.nodes('/root/questions/question/answer') x(o)
)
select stuff((select ','+answer from t for xml path('')),1,1,'') 'answers'
/*
answers
-------------------------
aaa,bbb,ccc,ddd
(1 row(s) affected)
*/
-- 以表的结果查询
select row_number() over(order by t.answer) 'id', t.answer
from
(select distinct o.value('.','varchar(10)') 'answer'
from A表
cross apply xmlcontent.nodes('/root/questions/question/answer') x(o)
) t
/*
id&