select 部门表.部门代码,部门表.部门名称,isnull(b.cou,0) as 小学,isnull(C.cou,0) as 本科
from 部门表
left outer join /*小学*/
(select count(*) as cou,部门代码 from 人员表 where 学历 = '小学' group by 部门代码) as
B on B.部门代码 = 部门表.部门代码
left outer join /*本科*/
(select count(*) as cou,部门代码 from 人员表 where 学历 = '本科' group by 部门代码) as
C on c.部门代码 = 部门表.部门代码
where 部门表.部门代码 like '101%'