一、高级查询

1、分组查询

1.1 SQL18 分组计算练习题

  • 描述

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

sql server 2016 分组后成绩按逗号分隔_面试


sql server 2016 分组后成绩按逗号分隔_大数据_02

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

输出:
male|北京大学|1|7.0|2.0
male|复旦大学|2|12.0|5.5
female|北京大学|1|12.0|3.0
female|浙江大学|1|5.0|1.0
male|山东大学|2|17.5|11.0

sql server 2016 分组后成绩按逗号分隔_面试_03

1.1.1 SQL语句(第一种解法)
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


sql server 2016 分组后成绩按逗号分隔_面试_04


sql server 2016 分组后成绩按逗号分隔_面试_05

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

sql server 2016 分组后成绩按逗号分隔_面试_06


sql server 2016 分组后成绩按逗号分隔_面试_07

1.2 SQL19 分组过滤练习题

  • 描述

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

sql server 2016 分组后成绩按逗号分隔_面试_08

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

sql server 2016 分组后成绩按逗号分隔_外链_09

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

sql server 2016 分组后成绩按逗号分隔_外链_10


sql server 2016 分组后成绩按逗号分隔_大数据_11

1.2.2 SQL语句(第二种解法推荐)

select