日期:2014-05-19  浏览次数:20368 次

求教一条SQL语句
我的表如下
表名Students字段id(学号),name(姓名),sex(性别),class(班级)
现在要查询所有男生比女生多的班级
小弟是SQL新人,希望高手指点一下,谢谢

------解决方案--------------------
select T.Class
from (
select
class,
sum(case when 性别 = '男 ' then 1 else 0 end) AS 男生人数,
sum(case when 性别 = '女 ' then 1 else 0 end) AS 女生人数
from Students
group by class ) T
where T.男生人数> T.女生人数