mysql练习--计算25岁以上和以下的用户数量

描述

题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
本题注意:age为null 也记为 25岁以下
 
示例:user_profile
id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male   复旦大学 4 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 male 26 复旦大学 3.6 9 6 52
 
根据示例,你的查询应返回以下结果:
age_cut number
25岁以下 4
25岁及以上 3

case --when-then的用法

select 班号,班名
case 系好
when 1 then ‘软件工程’
when 2 then '计算机'
end as 系号,班主任号
from 课表
 
select case when age < 25 or age is null then '25岁以下'
when age >= 25 then '25岁及以上'
end age_cut,count(*)number
from user_profile
group by age_cut

 

posted @ 2022-09-23 01:35  兴儿  阅读(90)  评论(0)    收藏  举报