什么数据库啊?不同的数据库写法不一样的
oracle:
select cid, 
       sum(decode(,'优',1,0)) 优,
       sum(decode(,'良',1,0)) 良
  from mytable
 group by cid;sql server:
select cid,
       sum(case when ='优' then 1 else 0 end case) 优,
       sum(case when ='良' then 1 else 0 end case) 良
  from mytable
 group by cid;

解决方案 »

  1.   

    Access
    select cid,
    sum(iif( ='优' ,1,0)) as 优,
    sum(iif( ='良' ,1,0)) as 良,
    from mytable
    group by cid
      

  2.   

    sql server:
    select cid,
    sum(case when  ='优'  then 1 else 0) as 优,
    sum(case when  ='良'  then 1 else 0) as 良,
    ...
    sum(case when  ='中'  then 1 else 0) as 中
    from mytable
    group by cid
    Access
    select cid,
    sum(iif( ='优' ,1,0)) as 优,
    sum(iif( ='良' ,1,0)) as 良     -----erickleung() 的这里多了逗号
    from mytable
    group by cid
      

  3.   

    transform count(*) select cid from mytable group by cid, pivot 
      

  4.   

    oracle:
    1.这种最简单:  
    select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
      from tname group by cid;2.这个也可以:
     select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
      select cid,,count(*) cnt
      from tname
      group by cid,,
      

  5.   

    oracle:
    1.这种最简单:  
    select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
      from tname group by cid;2.这个也可以:
     select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
    from (
      select cid,,count(*) cnt
      from tname
      group by cid,,
    )
      

  6.   

    oracle:
    1.这种最简单:  
    select cid,sumdecode(,'优',1,0)) 优,sum(decode(,'良',1,0)) 良
      from tname group by cid;2.这个也可以:
     select cid,sum(deocde(,'优',cnt,0)) 优,sum(deocde(,'良',cnt,0)) 良
    from(
      select cid,,count(*) cnt
      from tname
      group by cid,,
       )
      

  7.   

    vfp:
      select cid,sum(iif( ='优' ,1,0)) as 优,sum(iif( ='良' ,1,0)) as 良 from tablename group by cid
      

  8.   

    select cid,
    sum(case when  ='优'  then 1 else 0) as 优,
    sum(case when  ='良'  then 1 else 0) as 良,
    ...
    sum(case when  ='中'  then 1 else 0) as 中
    from mytable
    group by cid