题解 | #分群并计算群体人数#

分群并计算群体人数

https://www.nowcoder.com/practice/24319fd5c03e482c935783107114933d

select age_group, user_count
from 
(
select (case when age < 20 then '20以下' end) age_group,
    sum(case when age < 20 then 1 else 0 end) user_count,
    4 'r'
from customers_info
group by age_group

union
select (case when age > 50 then '50以上' end) age_group,
    sum(case when age > 50 then 1 else 0 end) user_count,
    2 'r'
from customers_info
group by age_group

union
select (case when age is null then '未填写'end) age_group,
    sum(case when age is null then 1 else 0 end) user_count,
     3 'r'
from customers_info
group by age_group

union
select (case when age > 20 and age < 50 then '20-50' end) age_group,
    sum(case when age > 20 and age < 50 then 1 else 0 end) user_count,
    1 'r'
from customers_info
group by age_group
)t 
where age_group is not null
order by r

#             when age > 50 then '50以上'
#             when age is null then '未填写'
#             else '20-50'
#         end age_group,
# from customers_info

全部评论

相关推荐

玉无心❤️:发照片干啥 发简历啊
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务