これを試して :
Declare @demo table(FieldName varchar(100))
insert into @demo values('1,8,2,3,4,10,5,9,6,7')
insert into @demo values('1,8')
insert into @demo values('1,8')
select ID, COUNT(id) ID_count from
(SELECT
CAST(Split.a.value('.', 'VARCHAR(100)') AS INT) AS ID
FROM
(
SELECT CAST ('<M>' + REPLACE(FieldName, ',', '</M><M>') + '</M>' AS XML) AS ID
FROM @demo
) AS A CROSS APPLY ID.nodes ('/M') AS Split(a)) q1
group by ID
実行が速いため、Devartの答えが好きです。これは、ニーズに合わせて修正された以前の回答です。
Declare @col varchar(200)
SELECT
@col=(
SELECT FieldName + ','
FROM @demo c
FOR XML PATH('')
);
;with demo as(
select cast(substring(@col,1,charindex(',',@col,1)-1) AS INT) cou,charindex(',',@col,1) pos
union all
select cast(substring(@col,pos+1,charindex(',',@col,pos+1)-pos-1)AS INT) cou,charindex(',',@col,pos+1) pos
from demo where pos<LEN(@col))
select cou ID, COUNT(cou) id_count from demo
group by cou