截取字符串并统计
我有一个表t1
t1的一列content中这样存的数据
a,b,c,d
b,d
b,c,d
我想统计这列中
a一共出现的次数
b一共出现的次数
c一共出现的次数
d一共出现的次数
怎么统计方便?
没有分了,不好意思,谢谢
[解决办法]
字符拆分精华
[解决办法]
select col,count(*) as cntfrom(select substring(a.col,b.number,charindex(',',a.col+',',b.number)-b.number) colfrom t1 a,master..spt_values bwhere b.type = 'p' and b.number between 1 and len(a.col) and len(a.col) >= 1 and substring(','+a.col,b.number,1) = ',') tgroup by col
[解决办法]
declare @t1 table (content varchar(10))insert into @t1select 'a,b,c,d' union allselect 'b,d' union allselect 'b,c,d'select c1,count(1) as cnt from @t1 a left join (select 'a' as c1 union select 'b' union select 'c' union select 'd') b on charindex(','+b.c1+',',','+a.content+',')>0 group by c1/*c1 cnt---- -----------a 1b 3c 2d 3*/