sql查询一个班级中总共有多少人以及男女分别多少人

--创建视图

create  view StuClassView as 

  SELECT s.ID ,s.StuName ,s.StuAge ,s.StuAddress ,s.StuTel ,s.ClassId ,s.StuId,s.StuSex ,e.ClassName,e.ClassInfo,e.ClassFlag 

  FROM Classes as e left  join   Students as s on s.ClassId=e.ClassId





select sc.ClassName as '班级名称',count(sc.StuId) as '总人数' ,sum(case when sc.StuSex='' then 1 else 0 end) as '',

 sum(case when sc.StuSex='' then 1 else 0 end) as '' from StuClassView as sc  group by sc.ClassName

 

你可能感兴趣的:(sql)