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

分群并计算群体人数

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-21 13:41
点赞 评论 收藏
分享
07-19 13:28
长沙学院 Java
程序员小白条:你有面试就有希望,没面试自然就没希望,到时候就知道了,你问别人也没啥用处的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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