select a.专业,left(a.学号,4) 年级,count(1) 学生人数,
     sum(case when b.入党时间 is not null then 1 else 0 end) 党员人数,
     sum(case when b.入党时间 is not null and a.性别='女' then 1 else 0 end) 女党员人数,
     sum(case when b.入党时间 is not null and a.民族<>'汉' then 1 else 0 end) 少数民族党员人数,
from 学生信息表 a
left join 党员信息表 b
on a.学号=b.学号 
group by left(a.学号,4)