/* github */

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

执行结果:

 

posted on 2019-08-29 13:24  王精灵  阅读(6375)  评论(0编辑  收藏  举报