表A
....字段1.....
....AB.......
....A........
....BC.......
....C........
....D........像上边这样,字段1是多选题的答案字段,现在要统计出一共有多少个人选了A,多少个人选了B,多少个选了C等等类推最好得到下边的结果A  2
B  2
C  2
D  1如果还能去掉重复就更好了,呵呵,比如用户答了AAB,那么只计一次A,一次B谢谢啦~

解决方案 »

  1.   

    补充,还有选项E,如果没人选,那么最好结果如下A  2 
    B  2 
    C  2 
    D  1 
    E  0
      

  2.   

    select 'A',(select count(1) from 表A where charindex('A',字段1)>0) as 数量
    union all 
    select 'B',(select count(1) from 表A where charindex('B',字段1)>0) as 数量
    union all 
    select 'C',(select count(1) from 表A where charindex('C',字段1)>0) as 数量
    union all 
    select 'D',(select count(1) from 表A where charindex('D',字段1)>0) as 数量
      

  3.   


    ---测试数据---
    if object_id('[表A]') is not null drop table [表A]
    go
    create table [表A]([字段1] varchar(2))
    insert [表A]
    select 'AB' union all
    select 'A' union all
    select 'BC' union all
    select 'C' union all
    select 'D'
     
    ---查询---
    select 'A' as 答案,isnull((select count(1) from 表A where charindex('A',字段1)>0),0) as 数量
    union all 
    select 'B' as 答案,isnull((select count(1) from 表A where charindex('B',字段1)>0),0) as 数量
    union all 
    select 'C' as 答案,isnull((select count(1) from 表A where charindex('C',字段1)>0),0) as 数量
    union all 
    select 'D' as 答案,isnull((select count(1) from 表A where charindex('D',字段1)>0),0) as 数量
    union all
    select 'E' as 答案,isnull((select count(1) from 表A where charindex('E',字段1)>0),0) as 数量---结果---
    答案   数量          
    ---- ----------- 
    A    2
    B    2
    C    2
    D    1
    E    0(所影响的行数为 5 行)