当前位置:网站首页>你违规了吗?
你违规了吗?
2022-06-11 14:09:00 【猴子数据分析】

【面试题】
有三张表:“学生表”、“期末成绩表”、“违纪表”。



查询半年内没有违纪记录,并且期末考试总分为每个班级前10名的学生名单。
【解题思路】
可以把这个复杂业务问题,使用多维度拆解分析方法,拆解为以下3个子问题:
1)从“违纪表”中查询半年内有违纪记录的学生名单,这部分最终要过滤掉
2)每个班级每个学生的期末考试总分
3)每个班级期末考试总分排名前10名的学生名单
1. 没有违纪记录的学生
查询半年内有违纪记录的学生名单,涉及到对时间的筛选。
问题中没有对“半年内”进行定义,这里我们可以自己定义下业务含义为“当前日期前半年(182天)之后”。

1)curdate()函数:获取当前日期;
2)date_sub(指定日期,interval … day):获取指定日期…天之前的日期。
“当前日期前182天”,也就是:
date_sub(curdate(),interval 182 day)从“违纪表”中查询半年内有违纪记录的学生名单:
select distinct 学生id
from 违纪表
where 违纪时间 >= date_sub(curdate(),interval 182 day);查询结果:

把这条查询结果记为表t1。
2.每个班级每个学生的期末考试总分
这里涉及到“期末成绩表”和“学生表”两张表,要用到多表查询。


把“期末成绩表”当作左表,使用左联结,给左表中补充班级id信息
select a1.*,
a2.班级id
from 期末成绩表 as a1
left join
学生表 as a2
on a1.学生id = a2.学生id;把上面的查询结果记录为表a3,然后,计算每个班级每个学生的期末考试总分。涉及到“每个”,要想到《猴子 从零学会SQL》里讲过的用“分组汇总”,按班级、学生分组(group by),汇总(分数求和sum)
select 班级id,学生id,
sum(分数) as 总分
from a3
group by 班级id,学生id;查询结果:

3.每个班级期末考试总分排名前 10 名的学生名单
涉及到排名问题,要想到《猴子从零学会SQL》里讲过的用窗口函数。把上一步查询结果记录为表a4,用窗口函数row_number()
select *,
row_number() over(partition by 班级id
order by 总分 desc) as 班级排名
from a4;查询结果:

把这条查询结果记为表a5。
4. 条件筛选
题目中设定了两个查询条件:
条件一:班级排名 <= 10;
条件二:要求“半年内没有违纪记录的学生”-->通过排除“半年内有违纪记录的学生”进行过滤。
select 学生id,总分,班级排名,班级id
from a5
where 班级排名 <= 10
and 学生id not in t1;将子查询代入:
select 学生id,总分,班级排名,班级id
from (
select *,
row_number() over(partition by 班级id
order by 总分 desc) as 班级排名
from (
select 班级id,学生id,
sum(分数) as 总分
from (
select a1.*,
a2.班级id
from 期末成绩表 as a1
left join
学生表 as a2
on a1.学生id = a2.学生id
) as a3
group by 班级id,学生id
) as a4
) as a5
where 班级排名 <= 10
and 学生id not in (
select distinct 学生id
from 违纪表
where 违纪时间 >= date_sub(curdate(),interval 182 day)
);
【本题考点】
1)考查对日期函数的了解;
2)考查对分组函数group by的灵活使用;
3)考查对多表联结的了解,特别是灵活使用where条件进行数据过滤。


️ 点击「阅读原文」
免费报名 数据分析训练营
边栏推荐
- Vi LXD deployment of lab server for development records
- VIM secondary replacement
- Live800:智能客服提升客户体验的几种方式
- 基于Qt开发实现的任务管理器
- Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
- XXVIII - 3D point cloud real-time and offline generation of 2D grid and 3D grid map
- 2022 Gansu Province safety officer B certificate test question simulation test question bank and online simulation test
- Current situation and future development trend of scaffold market in the world and China from 2022 to 2028
- 使用cpolar远程办公(1)
- 可变参表达式
猜你喜欢

YOLOv3学习笔记:YOLOv3的模型结构

Work summary: it took a long time to write SQL because of Cartesian product problem (Cartesian product summary attached)

可变参表达式

基于FPGA的VGA协议实现

Task manager based on Qt development

Ali talked about the use of strategic mode in the project

How to quickly compress the size of video?

cadence SPB17.4 - group operation(add to group, view group list, delete group)

非常值得学习的调度开源库推荐

The global mobile phone market is declining, and even apple does not expect too much of the iphone14
随机推荐
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
NoSQL之Redis配置与优化
In depth research and analysis report on global and Chinese diet food market
Private collection project practice sharing [Yugong series] February 2022 wechat applet -app Other properties of JSON configuration properties
解决循环依赖BUG。Relying upon circular references is discouraged and they are prohibited by default.
What's a good gift for the goddess Festival on March 8? Goddess Day gift sharing
Sqlmap detection SQL lab range
Leetcode 1962. 移除石子使总数最小(应该是向上取整)
提取式存储才是最佳的记忆方法
Leetcode 1963. Minimum number of swaps to balance strings (learning)
In depth research and analysis report on global and Chinese spray drying machinery market
AGV robot RFID sensor ck-g06a and Siemens 1200plc Application Manual
[Clickhouse] the clckhouse view can be inserted but not queried
gensim.models word2vec 参数
Task manager based on Qt development
Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended
Ali, tell me about the application scenarios of message oriented middleware?
Current situation and future development trend of precision air conditioning market in the world and China
非常值得学习的调度开源库推荐
Energy storage operation and configuration analysis of high proportion wind power system (realized by Matlab)