表 T如下:
AutoID   rec
1        1,2,3,4,5,56
2        3,4,56,7,9
3        1,2,3,5,6,7
4        2,4,5,6,78,9
........
我 现在要得到每个数字出现的次数,如:
数字    出现次数
1       2
2       3
3       3
4       3
5       3
6       1
.......
用什么简单的方法可以实现 ?

解决方案 »

  1.   

    但个数字的到是好求使用charindex就成
    统计全部的就只能拆分了
    方法:
    利用replace函数与动态语句结合。
    -- test:
    create table #a(id int identity(1,1),name varchar(100))
    create table #b(id int,name varchar(100))
    insert #a(name)
    select 'a,b,c'
    union all select 'd,e,f'
    union all select 'g,h'
    go
    declare @sql varchar(8000)
    set @sql = ''
    select @sql = @sql + ' insert into #b select ' + cast(id as varchar) + ',''' + replace(name,',',''' union all select ' + cast(id as varchar) + ',''') + '''' from #a
    --print @sql
    exec (@sql)
    go
    select * from #b
    go
    drop table #a,#b