select Class_fk as 班级,
查早操到岗次数=sum(case when MornExercise='到岗' then 1 else 0 end),
查早操缺岗次数=sum(case when MornExercise='缺岗' then 1 else 0 end),
查公寓到岗次数=sum(case when ApartCheck='到岗' then 1 else 0 end),
查公寓缺岗次数=sum(case when ApartCheck='缺岗' then 1 else 0 end)
from tb
group by Class_fk
------解决方案--------------------