当前位置: 首页> 房产> 市场 > 洛阳bbs_网站营销推广方案_凡科网站官网_兰州正规seo整站优化

洛阳bbs_网站营销推广方案_凡科网站官网_兰州正规seo整站优化

时间:2025/7/9 18:08:02来源:https://blog.csdn.net/2301_76890677/article/details/144403780 浏览次数:0次
洛阳bbs_网站营销推广方案_凡科网站官网_兰州正规seo整站优化

目录

SQL16--查找GPA最高值

描述

示例1

答案

其他方法:

SQL17--计算男生人数以及平均GPA

描述

示例1

答案

SQL18--分组计算练习题

描述

示例1

答案

SQL19--分组过滤练习题

描述

示例1

答案

SQL20--分组排序练习题

描述

示例1

 答案


SQL16--查找GPA最高值

描述

题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据

示例:某user_profile表如下:

iddevice_idgenderageuniversitygpa
12234male21北京大学3.2
22235maleNULL复旦大学3.8
32236female20复旦大学3.5
42237female23浙江大学3.3
52238male25复旦大学3.1
62239male25北京大学3.6
72240maleNULL清华大学3.3
82241femaleNULL北京大学3.7

根据输入,你的查询应返回以下结果,结果保留到小数点后面1位

gpa
3.8

示例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);
INSERT INTO user_profile VALUES(1,2234,'male',21,'北京大学',3.2);
INSERT INTO user_profile VALUES(2,2235,'male',null,'复旦大学',3.8);
INSERT INTO user_profile VALUES(3,2236,'female',20,'复旦大学',3.5);
INSERT INTO user_profile VALUES(4,2237,'female',23,'浙江大学',3.3);
INSERT INTO user_profile VALUES(5,2238,'male',25,'复旦大学',3.1);
INSERT INTO user_profile VALUES(6,2239,'male',25,'北京大学',3.6);
INSERT INTO user_profile VALUES(7,2240,'male',null,'清华大学',3.3);
INSERT INTO user_profile VALUES(8,2241,'female',null,'北京大学',3.7);

输出:

gpa
3.8

答案

select max(gpa) gpa from user_profile where university='复旦大学';

其他方法:

select gpa
from
(select gpa,
row_number()over(partition by university order by gpa desc) as ranking
from user_profile
where university = '复旦大学') as t
where t.ranking = 1;

使用的是窗口排序函数加子查询解题

用row_number()over() 在学校组里对GPA进行倒序排序 再增加条件rank=1 此时为GPA最大值

select gpa
from user_profile
where university = '复旦大学'
order by gpa DESC
limit 1

对gpa使用倒序排序,然后取第一条就是最大值

SQL17--计算男生人数以及平均GPA

描述

题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。

示例:user_profile

iddevice_idgenderageuniversitygpa
12138male21北京大学3.4
23214male复旦大学4.0
36543female20北京大学3.2
42315female23浙江大学3.6
55432male25山东大学3.8
62131male28北京师范大学3.3

根据输入,你的查询应返回以下结果,结果使用round保留到小数点后面1位

male_numavg_gpa
43.6

示例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);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8);
INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);

输出:

4|3.6

答案

select count(gender) as male_num,
round(avg(gpa),1) as avg_gpa from user_profile
where gender='male';

使用round(column,1)函数进行平均数的四舍五入,保留1位小数

SQL18--分组计算练习题

描述

题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

用户信息表:user_profile

30天内活跃天数字段(active_days_within_30)

发帖数量字段(question_cnt)

回答数量字段(answer_cnt)

iddevice_idgenderageuniversitygpaactive_days_within_30question_cntanswer_cnt
12138male21北京大学3.47212
23214male复旦大学4.015525
36543female20北京大学3.212330
42315female23浙江大学3.6512
55432male25山东大学3.8201570
62131male28山东大学3.315713
74321male26复旦大学3.69652

第一行表示: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升序排列:

解释:

第一行表示:北京大学的男性用户个数为1,平均活跃天数为7天,平均发帖量为2

。。。

最后一行表示:山东大学的男性用户个数为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(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;

select 查询结果 [性别;学校;count(设备ID) as 用户数;avg(30天内活跃记录) as 平均活跃天数;avg(发帖记录) as 平均发帖数]
from 从哪张表中查找数据 [user_profile]
group by 分组 [学校;性别]

SQL19--分组过滤练习题

描述

题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。

示例:user_profile

第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4在过去的30天里面活跃了7天,发帖数量为2,回答数量为12
。。。
最后一行表示:id为7的用户的常用信息为使用的设备id为4321,性别为男,年龄26岁,复旦大学,gpa为3.6在过去的30天里面活跃了9天,发帖数量为6,回答数量为52

根据示例,你的查询应返回以下结果,注意返回的字段名需要保持一致,同时保留3位小数(系统后台也会自动校正),3位之后四舍五入:

解释: 平均发贴数低于5的学校或平均回帖数小于20的学校有2个

属于北京大学的用户的平均发帖量为2.500,平均回答数量为21.000

属于浙江大学的用户的平均发帖量为1.000,平均回答数量为2.000

示例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` 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

答案

select university,round(avg(question_cnt),3) as avg_question_cnt,
round(avg(answer_cnt),3) as avg_answer_cnt
from user_profile 
group by university
having avg_question_cnt < 5 or avg_answer_cnt < 20;

聚合函数结果作为筛选条件时,不能用where,而是用having语法

SQL20--分组排序练习题

描述

题目:现在运营想要查看不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列,请你取出相应数据。

示例:user_profile

iddevice_idgenderageuniversitygpaactive_days_within_30question_cntanswer_cnt
12138male21北京大学3.47212
23214male复旦大学4.015525
36543female20北京大学3.212330
42315female23浙江大学3.6512
55432male25山东大学3.8201570
62131male28山东大学3.315713
74321female26复旦大学3.69652

第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4在过去的30天里面活跃了7天,发帖数量为2,回答数量为12
。。。
最后一行表示:id为7的用户的常用信息为使用的设备id为4321,性别为男,年龄26岁,复旦大学,gpa为3.6在过去的30天里面活跃了9天,发帖数量为6,回答数量为52

根据示例,你的查询应返回以下结果:

universityavg_question_cnt
浙江大学1.0000
北京大学2.5000
复旦大学5.5000
山东大学11.0000

示例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` 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);

输出:

university|avg_question_cnt
浙江大学|1.0000
北京大学|2.5000
复旦大学|5.5000
山东大学|11.0000

 答案

select university,avg(question_cnt) as avg_question_cnt
from user_profile
group by university
order by avg_question_cnt;

sql运行顺序

(1)from   (2) join   (3) on   (4) where   (5)group by   (6) avg,sum.... (组函数)   (7)having   (8) select   (9) distinct   (10) order by

  1. 当题目出现关键词“每”,“各”的时候,我们就可以判断结果集是需要进行分组的,我们就想到利用分组函数来解答我们的需求

  2. 可能有人问,为啥后面用having ,不能用where吗?

这里需要对聚合函数的结果进行比较,where后面是不能使用聚合函数的字段的。

  1. tips:在排序中order by 字段 asc --其中升序为asc(可以不写,默认就是按照升序排列),降序为desc(必须写)
关键字:洛阳bbs_网站营销推广方案_凡科网站官网_兰州正规seo整站优化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: