题解 | #分群并计算群体人数#
分群并计算群体人数
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