有一表上的数据像下面这样
姓名     成绩
小王      A
小王      A
小王      B
小王      B
小王      C
老李      A
老李      A
老李      A
老李      A
....
我要统计成这样显示出来,大家一般是怎么做的?
姓名     A    B    C
小王     2    2    1
老李     4    0    0
....

解决方案 »

  1.   

    查询我知道
    可以得到这样的结果
    姓名   成绩    统计
    小王   A       2
    小王   B       2
    小王   C       1
    老李   A       4
    ...
    可是怎么用DBGrid显示?
      

  2.   

    我一般的做法是创建临时表,字段为姓名,A,B,C
    然后统计将数据加入临时表中
      

  3.   

    我可以用sql server存储过程.
         select 姓名,
                     sum(case 成绩 when 'A' then 1 end)'A',
                     sum(case 成绩 when 'B' then 1 end)'B',
                     sum(case 成绩 when 'C' then 1 end)'C'
    from 表
    group by 姓名
      

  4.   

    如果用 Access 又不可以写存储过程。要怎么实现哟
      

  5.   

    草飘飘:查出那样的结果是没办法用DBGRID显示出来的,临时表不合适,
    不如直接查出你想要的结果
    select distinct 姓名,
    (select count(*) from table as t1 where t1.成绩=‘A’ and t1.姓名=t.姓名)as 'A',
    (select count(*) from table as t2 where t2.成绩=‘B’ and t2.姓名=t.姓名)as 'B',
    (select count(*) from table as t3 where t3.成绩=‘C’ and t3.姓名=t.姓名)as 'C',
    from table as t
    或者象剑风那样,可以不写存储过程。