select specialityname  ,defecttype ,count(*),
(select count(*) from detable a where a.dispeled=1 and a.specialityname = detable.specialityname and a.defecttype = detable.defecttype ) as tt,
(select count(*) from detable b where b.dispeled=1 and b.specialityname = detable.specialityname and b.defecttype = detable.defecttype ) as ss
from detable group by specialityname,defecttype

解决方案 »

  1.   

    select specialityname  ,defecttype ,count(*),sum(case when dispeled=1 then 1 else 0 end) tt,sum(case when dispeled=0 then 1 else 0 end) ss
    from detable  group by specialityname,defecttype
      

  2.   

    select specialityname  ,defecttype ,count(*)
    sum(case when dispeled=1 then 1 else 0 end ) as tt,
    sum(case when dispeled=0 then 1 else 0 end ) as ss
    from  detable  
    group by specialityname,defecttype
      

  3.   

    可以用case做交叉报表,see help
      

  4.   

    select specialityname  ,defecttype ,count(*),
    sum(case when  dispeled=1 then 1 else 0 end ) as tt,
    sum(case when  dispeled=0 then 1 else 0 end) as ss
    from detable  group by specialityname,defecttype