sql分组统计
学生表表结构
从左往右字段名称含义为:主键、姓名、学号、性别(1男,0女),班级Id
统计每个班级的男女学生人数和总人数
SELECT classId, SUM ( CASE WHEN sex = 1 THEN 1 ELSE 0 END ) AS '男', SUM ( CASE WHEN sex = 0 THEN 1 ELSE 0 END ) AS '女' , COUNT(0) AS '总人数' FROM student GROUP BY classId
执行结果:
欢迎评论留言交流