计算25岁以上和以下的用户数量
题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
本题注意:age为null 也记为 25岁以下
问题相关代码,请勿粘贴截图
运行结果及报错内容
select '25岁以下',count(*)
from user_profile
where age < 25 or age is null
union all
select '25岁及以上',count(*)
from user_profile
where age >= 25;
——这个是运行成功的,但是没有用到条件函数
select case when age < 25 or age is null then "25岁以下"
when age >= 25 then "25岁及以上"
end as age_cut,
count(*) number
from user_profile
group by age_cut;
——这是用的mysql的写法,也成功了
我的解答思路和尝试过的方法
我想尝试用Oracle的写法,但是怎么尝试都不成功,有没有办法能用Oracle的语法实现成功?
select age_cut,
count(age_cut) number
from (select case when age < 25 or age is null then '25岁以下'
when age >= 25 then '25岁及以上'
end age_cut
from user_profile)
group by age_cut;
我想要达到的结果
我上面的这个写法,一直提示我非单组分组函数,括号from()里面的是运行成功了,但是外面的就一直是报错的,我想要的结果就是
输出:
25岁以下|4
25岁及以上|3
我想在mysql里可以用case when 那在oracle里应该也可以用case when 或者说还是有其它的条件函数可以实现,最后统计用户数量的时候怎么都统计不出来,不知道是哪里有问题?求解啊!