select
gender,
university,
count(gender) user_num,
round(
sum(active_days_within_30) / count(active_days_within_30),
1
) avg_actie_day,
round(sum(question_cnt) / count(question_cnt), 1) avg_question_cnt
from
user_profile
group by
gender,
university
![在这里插入代码片](https://img-blog.csdnimg.cn/21d71970ec794ab5b7f94b4937e5d7cf.png)
1.1.1 SQL语句(第二种解法推荐)
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
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
1.2 SQL19 分组过滤练习题
- 描述
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
- 示例1
输入:
drop table if exists user_profile;
CREATE TABLEuser_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
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);
输出:
university|avg_question_cnt|avg_answer_cnt
北京大学|2.500|21.000
浙江大学|1.000|2.000
输入:
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` int ,
`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);
输出:
university|avg_question_cnt|avg_answer_cnt
北京大学|2.500|21.000
浙江大学|1.000|2.000
1.2.1 SQL语句(第一种解法)
select
university,
round(sum(question_cnt) / count(question_cnt), 3) avg_question_cnt,
round(sum(answer_cnt) / count(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
round(sum(question_cnt) / count(question_cnt), 3) < 5
or round(sum(answer_cnt) / count(answer_cnt), 3) < 20
select
university,
round(sum(question_cnt) / count(question_cnt), 3) avg_question_cnt,
round(sum(answer_cnt) / count(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
round(sum(question_cnt) / count(question_cnt), 3) < 5
or round(sum(answer_cnt) / count(answer_cnt), 3) < 20
1.2.2 SQL语句(第二种解法推荐)
select
university,
round(avg(question_cnt), 3) avg_question_cnt,
round(avg(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
avg(question_cnt) < 5
or avg(answer_cnt) < 20
select
university,
round(avg(question_cnt), 3) avg_question_cnt,
round(avg(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
avg(question_cnt) < 5
or avg(answer_cnt) < 20
1.3 SQL20 分组排序练习题
描述
题目:现在运营想要查看不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列,请你取出相应数据。
- 示例1
输入:
drop table if exists user_profile;
CREATE TABLEuser_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
);
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);
输出:
浙江大学|1.0000
北京大学|2.5000
复旦大学|5.5000
山东大学|11.0000
输入:
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` int ,
`question\_cnt` int ,
`answer\_cnt` int
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
![img](https://img-blog.csdnimg.cn/img_convert/33caa75e24238d4993679999c343ed5b.png)
![img](https://img-blog.csdnimg.cn/img_convert/473b21a4a4ff18eee47ce0738e6c42e4.png)
![img](https://img-blog.csdnimg.cn/img_convert/ae2cbcaf39a6bc8baf8c92d31cd2224f.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
(img-hFRWfhiV-1714737980953)]
[外链图片转存中...(img-Shlfrr7K-1714737980953)]
[外链图片转存中...(img-Rte9lWca-1714737980954)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**