首页 > 试题广场 >

计算25岁以上和以下的用户数量

[编程题]计算25岁以上和以下的用户数量
  • 热度指数:419432 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
本题注意:age为null 也记为 25岁以下

示例:user_profile
id device_id gender age university gpa active_days_within_30
question_cnt
answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 male 26 复旦大学 3.6 9 6 52

根据示例,你的查询应返回以下结果:
age_cut number
25岁以下 4
25岁及以上
3

示例1

输入

drop table if exists `user_profile`;
drop table if  exists `question_practice_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int 
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);

INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');

输出

25岁以下|4
25岁及以上|3
推荐
本题考的是条件函数if。if(x=n,a,b)表示如果x=n,则返回a,否则就是b了。
代码:
Select
if(age>=25,'25岁及以上','25岁以下' ) as age_cut,
count(device_id) as number
From user_profile
Group by age_cut
//牛客网似乎没有对执行顺序做限制。但是很多数据库是不能在group by中 用所在表中不存在的列的。所以要把前面的结果,做成派生表,也就是要套一层壳。 

针对这题,更好的答案如下:
select age_cut,count(device_id)as number 
from(Select if(age>=25,'25岁及以上','25岁以下' )as age_cut,device_id 
From user_profile)u2
Group by age_cut



编辑于 2021-11-03 11:48:16 回复(22)
这个知识点我没看过,┭┮﹏┭┮
发表于 2021-09-08 09:29:52 回复(7)
参考讨论区的3种方法,但我不理解为什么group by后面可以用age_cut,不是先执行groupby再执行select吗?我去百度也没找到原因
1.
select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cut, 
count(device_id) as number
from user_profile
group by age_cut
2.
SELECT 
IF(age>=25,'25岁及以上','25岁以下') AS age_cut, 
COUNT(device_id) AS number 
FROM user_profile 
GROUP BY age_cut 
3.
select '25岁以下' as age_cut,count(device_id) as number
from user_profile
where age<25 or age is null
union all
select '25岁及以上' as age_cut,count(device_id) as number
from user_profile
where age>=25;

发表于 2021-11-01 17:00:49 回复(24)
select '25岁以下',count(device_id)
from user_profile
where age<25 or age is null
union all
select '25岁以及上',count(device_id)
from user_profile
where age>=25;

联合表也可以的,我刚才是打错字了,我去
发表于 2021-08-25 10:43:19 回复(22)
select 
    (case
        when age>=25 then '25岁及以上'
        else '25岁以下'
    end) as age_cut, 
    count(device_id)
    
    from user_profile
    group by age_cut

编辑于 2021-11-01 19:46:57 回复(8)
select
case when age >= 25 then "25岁及以上" else "25岁以下" end as age_cut ,
count(*) as number
from user_profile
group by case when age >= 25 then "25岁及以上" else "25岁以下" end
-- mysql可以直接在groupby后面使用别名。

这个题非常明显的是想考察case when 的用法,由于NULL被归到25岁以下,所以需要先判断 25岁以上的,剩下的就是25岁以下的。

难点在于,没有学过的话,可能不会想到case when 还可以参与分组,放到
group by 后面

发表于 2022-07-14 19:30:48 回复(2)
通过的代码,是在group by age之后不对自己试出来的:
SELECT case
        when age<25 OR age is null then '25岁以下'
        when age>=25 then '25岁及以上'
        else 0
        end as age_cut
,COUNT(id)
FROM user_profile
GROUP BY age_cut
但我自己的疑问:在group by执行的时候,select还没执行,它怎么会知道age_cut是指什么呢?
发表于 2021-10-24 23:34:58 回复(8)
SELECT 
    case 
        when age >= 25 then '25岁及以上'
        else '25岁以下'
    end as age_cut,
    count(device_id) as number
FROM user_profile
GROUP by age_cut;

编辑于 2021-11-01 19:46:40 回复(6)
哦 我终于发现这个题的坑点了 他要求的输出是 '25岁及以上' 结果必须打'25岁以及上'才能对。。。
发表于 2021-08-26 09:39:42 回复(13)
SELECT 
    IF(age < 25 OR age IS NULL, "25岁以下", "25岁及以上") AS age_cut,
    COUNT(id) AS num
FROM
    user_profile
GROUP BY 
    age_cut
发表于 2021-10-02 21:31:23 回复(4)
##### 方法1 
## 通过union 连接两种选出的表
select 
    "25岁以下" as age_cut, 
    count(device_id) as number
from 
    user_profile
where 
    (age < 25)&nbs***bsp;(age is null )

union all 

select 
    "25岁及以上" as age_cut, 
    count(device_id) as number
from 
    user_profile
where 
    age >= 25
;

####### 方法2 
## 使用 case end 完成筛选并对每个行进行命名 + group by 完成聚合操作 
select 
    case when (age < 25)&nbs***bsp;(age is null) then "25岁以下"
         when (age >= 25) then "25岁及以上"
    end age_cnt, 
    count(device_id) as number
from 
    user_profile
group by 
    age_cnt 
;


select 
    case when (age < 25)&nbs***bsp;(age is null) then "25岁以下"
         else "25岁及以上"
    end age_cnt, 
    count(device_id) as number
from 
    user_profile
group by 
    age_cnt 
;

### 方法4 
### 使用 if(条件,输出情况1,输出情况2) 完成数据的判断 
select 
    if (age >= 25, '25岁及以上','25岁以下') as age_cut, 
    count(device_id) as number 
from 
    user_profile
group by 
    age_cut
;






发表于 2022-05-10 19:51:35 回复(1)

也可以用case when 当age>25 就执行25岁以上,否则就是小于25岁,一分组统计就行了。
Select
case when age>=25 then '25岁以及上'
else '25岁以下' end 
as age_cut,
count(case when age>=25 then '25岁以及上'
else '25岁以下' end) as number
From user_profile
Group by age_cut
弄了一版容易版本,吐槽这答案字体,我弄半天
发表于 2021-08-25 16:36:13 回复(2)

select if(age>=25,'25岁及以上','25岁以下' ) age_cut ,count(*) number
from user_profile
group by age_cut
搞了好久才算明白这道题目的关键点:
1.用条件函数if()重新分组;
2.用count(*)计数统计,这个函数会将null也计入
发表于 2022-07-16 10:11:16 回复(1)
select '25岁以下' as age_cut, count(device_id) as number
from user_profile
where age<25 or age is null
union all
select '25岁及以上'as age_cut, count(device_id) as number
from user_profile
where age>=25
group by age_cut

发表于 2022-03-25 08:28:17 回复(0)
SELECT '25岁以下',SUM(IFNULL(age,24)<25)
FROM user_profile
UNION
SELECT '25岁及以上',SUM(IFNULL(age,24)>=25)
FROM user_profile;
发表于 2022-01-11 21:01:34 回复(0)
看了好久才发现,原来age中有空值,空值也要考虑进来。
#用IF
SELECT
IF(age < 25&nbs***bsp;age IS NULL,'25岁以下','25岁及以上') AS age_cut,
COUNT(device_id) as number
FROM user_profile
GROUP BY age_cut;

#用union
SELECT '25岁以下' age_cut,COUNT(device_id) NUMBER
FROM user_profile
WHERE age < 25&nbs***bsp;age IS NULL
UNION
SELECT '25岁及以上' age_cut,COUNT(device_id) NUMBER
FROM user_profile
WHERE age >= 25;


发表于 2021-09-24 11:15:43 回复(0)
这一题学会IF函数的用法,检查好久才看到在if函数后面忘记用逗号隔开,一直显示错误。提醒自己要细心!
我的答案:
select
if(age>=25,'25岁及以上','25岁以下' ) as age_cut
count(device_id) as number
from user_profile
group by age_cut
正确答案:
Select
if(age>=25,'25岁及以上','25岁以下' ) as age_cut,
count(device_id) as number
From user_profile
Group by age_cut
编辑于 2021-11-01 19:47:33 回复(0)
select
case
    when age>=25 then '25岁及以上'
    else '25岁以下'
    end as age_cut,
    COUNT(device_id) as NUMBER
from
user_profile
GROUP BY
age_cut;
发表于 2022-03-12 11:15:33 回复(1)
请问各位大佬,为什么报错
SELECT '25岁以下' as age_cut,COUNT(device_id) as number
FROM user_profile
WHERE age<25 or age is null
union
SELECT '25岁及以上' as age_cut,COUNT(device_id) as number
FROM user_profile
WHERE age>=25

发表于 2022-10-05 10:52:00 回复(2)
方法1(Oracle不支持):
select
 if(age >= 25,'25岁及以上','25岁以下') as age_cut,
 count(device_id)
from user_profile
group by age_cut;
方法2(实用):
select
  '25岁以下' as age_cut,
  count(*) as number
from
  user_profile
where
  age < 25
  or age is null
union all
select '25岁及以上' as age_cut,
  count(*) as number
from
  user_profile
where
  age >= 25;
方法3:
select
   (case when age >= 25 then '25岁及以上' else '25岁以下' end)as age_cut,
  count(device_id) as number
from
  user_profile
group by age_cut



发表于 2022-08-10 10:41:56 回复(0)
--方法一:联接查询
SELECT
    '25岁以下' as age_cut,
    count(device_id) as number
from user_profile
where age<25&nbs***bsp;age is null
union
SELECT
    '25岁及以上' as age_cut,
    count(device_id) as number
from user_profile
where age >= 25;

--方法二:if语句
select 
    age_cut,
    count(a.device_id) as number
FROM
(
SELECT
    device_id,
    if(age>=25,'25岁及以上','25岁以下') as age_cut
from user_profile
) as a
group by age_cut

发表于 2022-02-28 15:30:46 回复(0)