如有一表(T1):C1
---
A
AB
BC
C
D
ABCD
D
AB
CD
如何能获取到下面的结果(单个字符出现的次数)?
A    4
B    4
C    4
D    4(注:不是固定的ABCD,可能有……………………N种)

解决方案 »

  1.   

    select c1,次数=count(c1)from tb
    group by c1
      

  2.   

    楼上的,我需要的是串里单个字符出现的次数
    你的结果得出来的是
    A    1
    AB   1
    ………………
      

  3.   

    select b.id,count(1) from T1 a,(select 'A' as id union select 'B' union select 'C'
            union select 'D' union select 'E' union select 'F') b
    where charindex(b.id,a.c1)>0 group by b.id
      

  4.   

    create table tb(id varchar(10))
    insert into tb values('A')
    insert into tb values('AB')
    insert into tb values('BC')
    insert into tb values('C')
    insert into tb values('D')
    insert into tb values('ABCD')
    insert into tb values('D')
    insert into tb values('AB')
    insert into tb values('CD')select id , count(1) cnt from
    (
      select id = substring(a.id , b.number , 1) 
      from tb a join master..spt_values  b 
      on b.type='p' and b.number between 1 and len(a.id)
    ) t
    group by iddrop table tb/*
    id   cnt         
    ---- ----------- 
    A    4
    B    4
    C    4
    D    4(所影响的行数为 4 行)
    */