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

分群并计算群体人数

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

全部评论

相关推荐

02-08 15:53
门头沟学院 Java
CoderEcho:让公司知道便宜没好货
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务