日期:2014-05-17 浏览次数:20596 次
if object_id('test') is not null drop table test
go
create table test(PersonID int,AttnIDs varchar(20))
go
insert into test
select 18,'13 10' union all
select 15,'16 15' union all
select 15,'9'
go
declare @xml xml,@str varchar(max)
select @str=isnull(@str,'')+'<x>'+convert(varchar(5),PersonID)+'</x><x>'+replace(replace(AttnIDs,convert(varchar(5),PersonID),''),' ','</x><x>')+'</x>' from test
select @xml=convert(xml,@str)
;with cte as
(
select N.v.value('.','varchar(10)') ID
from @xml.nodes('/x') N(v)
)
select ID,count(ID) cnt from cte
where ID<>''
group by ID
/*
(3 行受影响)
ID cnt
---------- -----------
10 1
13 1
15 2
16 1
18 1
9 1
(6 行受影响)
*/