首页 > 试题广场 >

分组计算练习题

[编程题]分组计算练习题
  • 热度指数:587402 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。


用户信息表:user_profile
30天内活跃天数字段(active_days_within_30)
发帖数量字段(question_cnt)
回答数量字段(answer_cnt)
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.0 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
第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4在过去的30天里面活跃了7天,发帖数量为2,回答数量为12
。。。
最后一行表示:id为7的用户的常用信息为使用的设备id为4321,性别为男,年龄26岁,复旦大学,gpa为3.6在过去的30天里面活跃了9天,发帖数量为6,回答数量为52


你的查询返回结果需要对性别和学校分组,示例如下,结果保留1位小数,1位小数之后的四舍五入,查询出来的结果按照gender、university升序排列
gender university user_num avg_active_day avg_question_cnt
female 北京大学 1 12.0 3.0
female
浙江大学 1 5.0 1.0
male 北京大学 1 7.0 2.0
male
复旦大学 2 12.0 5.5
male 山东大学 2 17.5 11.0

解释:
第一行表示:北京大学的男性用户个数为1,平均活跃天数为12天,平均发帖量为3
。。。
最后一行表示:山东大学的男性用户个数为2,平均活跃天数为17.5天,平均发帖量为11
示例1

输入

drop table if exists user_profile;
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` float,
`question_cnt` float,
`answer_cnt` float
);
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);

输出

gender|university|user_num|avg_active_day|avg_question_cnt
female|北京大学|1|12.0|3.0
female|浙江大学|1|5.0|1.0
male|北京大学|1|7.0|2.0
male|复旦大学|2|12.0|5.5
male|山东大学|2|17.5|11.0
select gender,university,
    count(*) as user_num,
    avg(active_days_within_30) as avg_active_day,
    avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university
order by gender;
 注意最后要对性别作排序,否则其他都对,整体输出顺序不一致出现错误。

发表于 2025-07-01 23:23:31 回复(0)
SELECT
       gender, university,
       COUNT(device_id) AS user_num,
       avg(active_days_within_30) AS avg_active_day,
       avg(question_cnt) AS avg_question_cnt
FROM user_profile
GROUP BY gender, university

为什么也不对
发表于 2025-06-29 16:48:16 回复(0)
select 
gender,university
,count(gender) as user_num
,round(avg(active_days_within_30),1) as avg_active_day
,round(avg(question_cnt),1) as avg_question_cnt
from user_profile
group by gender,university
order by gender,university
发表于 2025-06-08 15:46:42 回复(0)
select gender,university,count(device_id) as user_num,avg(active_days_within_30) as avg_active_day,avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university
order by gender,university
发表于 2025-06-07 11:37:41 回复(0)
SELECT
    gender,
    university,
    COUNT(device_id) AS user_num,
    ROUND(AVG(active_days_within_30),1) AS avg_active_day,
    ROUND(AVG(question_cnt),1) AS avg_question_cnt
FROM
    user_profile
GROUP BY
    gender,
    university
ORDER BY
    gender ASC,
    university ASC;
发表于 2025-06-05 13:42:43 回复(0)
select gender,
university,
count(id) as user_num,
round(avg(active_days_within_30),1) as avg_active_day,
round(avg(question_cnt),1) as avg_question_cnt
from user_profile
group by gender,university
order by gender asc,university asc;
谁懂啊做了好久,终于做对了
发表于 2025-05-28 17:54:03 回复(0)
select gender,university,
count(gender) as user_num,
round(avg(active_days_within_30),1) as avg_active_day,
round(avg(question_cnt),1) as avg_question_cnt
from user_profile
group by gender,university
order by gender,university;
我看有的人在order by 末尾加了ASC,有点没明白,mysql里面order by本身就是升序,降序是末尾加DESC
发表于 2025-05-27 22:54:33 回复(0)
貌似这样才是符合题目要求的。
select gender,university,count(gender) as user_num,round(avg(active_days_within_30),1) as avg_active_day,round(avg(question_cnt),1) as avg_question_cnt from user_profile group by university,gender order by gender,university asc;
发表于 2025-05-26 22:18:33 回复(0)
select gender,university,
count(id) user_num,
avg(active_days_within_30) avg_active_day,
avg(question_cnt) avg_question_cnt
from user_profile
group by university,gender
order by gender,university
分完组的话,数什么就无所谓了
发表于 2025-05-13 14:25:07 回复(0)
select gender,university,
count(device_id) as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university;
有没有大神说说哪里错了,老是运行错误
发表于 2025-05-13 00:41:14 回复(0)
select gender,university,count(*) as user_num,round(sum(active_days_within_30)/count(*),1) as avg_active_day,round(sum(question_cnt)/count(*),1) as avg_question_cnt
from user_profile 
group by university,gender
order by user_num
求指点,为什么我求出来的数据和答案是一样的,但是排序不一样。
我一开始以为是group by gender,university这个条件的顺序不一样导致的排序不一样,但是我两种排序都试过了,都会和答案的不一样。
发表于 2025-05-11 19:38:34 回复(0)
找不出错在哪里啊?为啥,求指点
select gender,university,count(device_id) as uesr_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by gender,university
order by gender
发表于 2025-05-08 21:56:46 回复(0)
select 
    gender,university,
    COUNT(device_id) as user_num,
    round(AVG(active_days_within_30),1) as avg_cative_day, 
    round(AVG(question_cnt),1) as avg_question_cnt
from user_profile
group by gender,university
order by gender,university;

这个为啥会不通过呢?明明和答案一模一样
发表于 2025-05-08 17:08:40 回复(0)
我这个为什么报错了
select 
    gender, university,
    count(device_id) as user_num,
    round(avg(active_days_within_30),1) as avg_active_days,
    round(avg(question_cnt),1) as avg_question_cnt
from user_profile
group by gender, university
order by gender,university

发表于 2025-04-28 23:17:07 回复(0)
select gender,
university,
count(gender) user_num,
round(AVG(active_days_within_30),1) avg_active_day,
round(AVG(question_cnt),1) avg_question_cnt
from user_profile
group by university,gender
order by gender,university;

别忘了order  by,不然也会报错
发表于 2025-04-21 12:12:09 回复(0)