sql中SUM与case when的组合使用

SELECT SUM(case WHEN sex=1 then 1 else 0 end )as '男生',SUM(case when sex =2 then 1 else 0 end )'女生'FROM asex

这句sql输出的就是男女生的人数(1代表男生,2代表女生),SUM(case WHEN sex=1 then 1 else 0 end )这句话的含义就是统计sex列中值为1的行数总和。

猜你喜欢

转载自blog.csdn.net/langwang1993/article/details/80736095