我有一个A表如下:
姓名   选项1    选项2  选项3  选项4
张三   ABCD      A      C       D
李四   AB        AB     BC      AB
....................................
王五   ABC       B      A       ACD最后我要统计选项1,选项2,选项3,选项4中的A、B、C、D选项各有多少,其查询的表如下:
选项    选项1  选项2   选项3  选项4
A        3       2      1      2
B        3       2      1      1  
C        2       0      2      1  
D        1       0      0      2请用一条sql的查询语句完成.
哈哈,也可以在以下论坛回答该问题,我个人主页上的论坛。
http://www.hepost.com/cgi-bin/bbs3000/bbs.cgi?id=200203231232

解决方案 »

  1.   

    可不可以这样做:
    adoquery1.sql.add('select sum(case when upper(选项1) like ''%A%'' end) as 选项A,sum(case when upper(选项1) like ''%B%'' end) as 选项B,sum(case when upper(选项1) like ''%C%'' end) as 选项 C...... from A表')
      

  2.   

    再用个表做个关联吧~~~~~~~~~~~Select 
           (Select isnull(count(*),0) from a where 选项1 like "%A%") as 选项1,
           (Select isnull(count(*),0) from a where 选项2 like "%B%") as 选项2,
           (Select isnull(count(*),0) from a where 选项3 like "%C%") as 选项3,
           (Select isnull(count(*),0) from a where 选项4 like "%D%") as 选项4 
    From a AA
      

  3.   

    select 选项='A' ,   
           选项1=(select count(*) from A where 选项1 like '%A%' ),
           选项2=(select count(*) from A where 选项2 like '%A%' ),
           选项3=(select count(*) from A where 选项3 like '%A%' ),
           选项4=(select count(*) from A where 选项4 like '%A%' )
    union
    select 选项='B' ,   
           选项1=(select count(*) from A where 选项1 like '%B%' ),
           选项2=(select count(*) from A where 选项2 like '%B%' ),
           选项3=(select count(*) from A where 选项3 like '%B%' ),
           选项4=(select count(*) from A where 选项4 like '%B%' )
    union
    .
    .
    .
      

  4.   

    非的用SQL吗,你可以把把选项1的字段值都连接起来,形成一个新的字符串,然后再统计不好吗?
      

  5.   

    liqianglqlq(银飞狐):      把字段连起来,还不一样要用SQL吗?可笑~~
      

  6.   

    对了,统计的表格形式可以变化,也可以用多条SQL语句,写出代码。谢谢各位。
      

  7.   

    select  'A',
    (select count(*) from ttt where charindex('A',a2)>0),
    (select count(*) from ttt where charindex('A',a3)>0),
    (select count(*) from ttt where charindex('A',a4)>0),
    (select count(*) from ttt where charindex('A',a5)>0)
    from ttt
    union
    select  'B',
    (select count(*) from ttt where charindex('B',a2)>0),
    (select count(*) from ttt where charindex('B',a3)>0),
    (select count(*) from ttt where charindex('B',a4)>0),
    (select count(*) from ttt where charindex('B',a5)>0)
    from ttt
    union
    select  'C',
    (select count(*) from ttt where charindex('C',a2)>0),
    (select count(*) from ttt where charindex('C',a3)>0),
    (select count(*) from ttt where charindex('C',a4)>0),
    (select count(*) from ttt where charindex('C',a5)>0)
    from ttt
    union
    select  'D',
    (select count(*) from ttt where charindex('D',a2)>0),
    (select count(*) from ttt where charindex('D',a3)>0),
    (select count(*) from ttt where charindex('D',a4)>0),
    (select count(*) from ttt where charindex('D',a5)>0)
    from ttt
      

  8.   

    tommy_linux(津工之鸟) 如果我不用sql 你相信吗?话说的别太绝
      

  9.   

    tommy_linux(津工之鸟)有本事你开个50分的帖子,我来说方法,如果可行,你给分怎么样;我知道你水平高,瞧不起人,我本来到这里就是来向大家学习的(也包括你),但我起码知道尊重每一个人(也包括你),尊重他们的每一个意见,不管对还是错。作程序就跟作数学一样,方法有许多,大家的思维各不相同,干吗非要拿你的思维来看待我说的方法呢,你又有什么把握完全否定我呢,我重声一遍:我可以不用一条SQL语句把这些字段值连接起来
      

  10.   

    tommy_linux(津工之鸟)帖子发了后短消息告诉我地址
      

  11.   

    select  'A',
    (select count(*) from ttt where charindex('A',a2)>0),
    (select count(*) from ttt where charindex('A',a3)>0),
    (select count(*) from ttt where charindex('A',a4)>0),
    (select count(*) from ttt where charindex('A',a5)>0)
    from ttt
    union
    select  'B',
    (select count(*) from ttt where charindex('B',a2)>0),
    (select count(*) from ttt where charindex('B',a3)>0),
    (select count(*) from ttt where charindex('B',a4)>0),
    (select count(*) from ttt where charindex('B',a5)>0)
    from ttt
    union
    select  'C',
    (select count(*) from ttt where charindex('C',a2)>0),
    (select count(*) from ttt where charindex('C',a3)>0),
    (select count(*) from ttt where charindex('C',a4)>0),
    (select count(*) from ttt where charindex('C',a5)>0)
    from ttt
    union
    select  'D',
    (select count(*) from ttt where charindex('D',a2)>0),
    (select count(*) from ttt where charindex('D',a3)>0),
    (select count(*) from ttt where charindex('D',a4)>0),
    (select count(*) from ttt where charindex('D',a5)>0)
    from ttt
      

  12.   

    tfxg(tfxg)你的答案怎么与票票的完全一样呢?