SQL case when 查询性别统计总数,为什么性别为空的数量是0

AI新视界 AI新视界 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
1/表结构:

表数据:

SQL:
use Text
------将sum与case结合使用,可以实现分段统计。如果现在希望将上表中各种性别的人数进行统计,sql语句如下:--------------
------为什么性别为空判断不出来???????????----------------------------
----------modify---------------
select
sum(case u.stu_sex when 1 then 1 else 0 end) as 男性,
sum(case u.stu_sex when 2 then 1 else 0 end)女性,
sum(case when u.stu_sex <> 1 and u.stu_sex <> 2 then 1 else 0 end) 性别为空
from T_Stuinfo u;
结果:


----或者----

select
count(case when u.stu_sex=1 then 1 end)男性,
count(case when u.stu_sex=2 then 1 end)女,
count(case when u.stu_sex <>1 and u.stu_sex<>2 then 1 end)性别为空
from T_Stuinfo u;
结果:


...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
AI新视界
MS-SQL Server 34,877 成员 254,635 内容