select class_ID,count(title),(select count(title) from 表A where title='A'), 
       (select count(title) from 表A where title='B') from 表A group by class_ID

解决方案 »

  1.   

    对不起,刚才看错了,呵呵,应该这样写select class_ID,(select count(title) from 表A where class_ID=a.class_ID),(select count(title) from 表A where title='A' and class_ID=a.class_ID), 
           (select count(title) from 表A where title='B' and  class_ID=a.class_ID) from 表A a group by class_ID
      

  2.   

    select class_ID,
           count(title) as titlenum,
           (select count(title) from 表A as a where a.class_ID =表A.class_ID and title='A') as Anum, 
           (select count(title) from 表A as a where a.class_ID =表A.class_ID and title='B') as Bnum  
    from 表A 
    group by class_ID
      

  3.   

    select CLASS_ID
           ,count(1) as 'titlenum'
           ,sum(case when TITLE='A' then 1 else 0 end) as 'Anum'
           ,sum(case when TITLE='B' then 1 else 0 end) as 'Bnum'
    from A
    group by CLASS_ID
    order by CLASS_ID
      

  4.   

    select
        CLASS_ID,
        titlenum  = sum(TITLE),
        Anum  = sum(case TITLE when 'A' then 1 else 0 end),
        Bnum  = sum(case TITLE when 'B' then 1 else 0 end)
        
    from   a group by  CLASS_ID