sql练习--查看不同年龄段的用户明细

描述

题目:现在运营想要将用户划分为20岁以下,20-24岁,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
 
根据示例,你的查询应返回以下结果:
device_id gender age_cut
2138 male 20-24岁
3214 male 其他
6543 female 20-24岁
2315 female 20-24岁
5432 male 25岁及以上
2131 male 25岁及以上
4321 male 25岁及以上

select device_id,gender,
case when age < 20 then '20岁以下'
when age is null then '其他'
when age >= 20 and age < 25 then '20-24岁'
when age >= 25 then '25岁及以上'
end age_cut
from user_profile
select device_id,gender,
case when age < 20 then '20岁以下'
when age >= 20 and age < 25 then '20-24岁'
when age >= 25 then '25岁及以上'
else '其他'
end age_cut
from user_profile

 

posted @   兴儿  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示