当前位置:网站首页>Did you break the rules?
Did you break the rules?
2022-06-11 14:31:00 【Monkey data analysis】

【 Interview questions 】
There are three tables :“ Student list ”、“ Final grade sheet ”、“ Disciplinary table ”.



There is no record of violation of discipline within half a year , And the final exam score is before each class 10 The list of students who are in the first place .
【 Their thinking 】
This complex business problem , Use the multi-dimensional disassembly analysis method , Disassemble into the following 3 Subproblem :
1) from “ Disciplinary table ” Query the list of students with disciplinary records within half a year , This part should be filtered out finally
2) The total final exam score of each student in each class
3) The total score of the final exam of each class ranks top 10 The list of students who are in the first place
1. Students who have no record of disciplinary violations
Check the list of students with disciplinary records within half a year , It involves the screening of time .
There is no right in the question “ Within half a year ” Define , Here we can define the business meaning by ourselves as “ Half a year before the current date (182 God ) after ”.

1)curdate() function : Get current date ;
2)date_sub( Specify Date ,interval … day): Get the specified date … Days before .
“ Before the current date 182 God ”, That is to say :
date_sub(curdate(),interval 182 day)from “ Disciplinary table ” Query the list of students with disciplinary records within half a year :
select distinct Student id
from Disciplinary table
where Time of violation >= date_sub(curdate(),interval 182 day);Query results :

Record this query result as a table t1.
2. The total final exam score of each student in each class
This involves “ Final grade sheet ” and “ Student list ” Two tables , Multiple table queries are required .


hold “ Final grade sheet ” As a left watch , Use the left link , Add classes to the left table id Information
select a1.*,
a2. class id
from Final grade sheet as a1
left join
Student list as a2
on a1. Student id = a2. Student id;Record the above query results as a table a3, then , Calculate the final exam score of each student in each class . involves “ Every ”, Think of it 《 monkey Learn from scratch SQL》 It's used in “ Group summary ”, By class 、 Students in groups (group by), Summary ( Sum fractions sum)
select class id, Student id,
sum( fraction ) as Total score
from a3
group by class id, Student id;Query results :

3. The total score of the final exam of each class ranks top 10 The list of students who are in the first place
When it comes to ranking , Think of it 《 Monkeys learn from scratch SQL》 The window function mentioned in . Record the query result of the previous step as a table a4, Using the window function row_number()
select *,
row_number() over(partition by class id
order by Total score desc) as Class ranking
from a4;Query results :

Record this query result as a table a5.
4. Condition screening
Two query conditions are set in the title :
Conditions for a : Class ranking <= 10;
Condition 2 : requirement “ Students who have no disciplinary record within half a year ”--> By excluding “ Students who have a disciplinary record within six months ” To filter .
select Student id, Total score , Class ranking , class id
from a5
where Class ranking <= 10
and Student id not in t1; Substitute subquery into :
select Student id, Total score , Class ranking , class id
from (
select *,
row_number() over(partition by class id
order by Total score desc) as Class ranking
from (
select class id, Student id,
sum( fraction ) as Total score
from (
select a1.*,
a2. class id
from Final grade sheet as a1
left join
Student list as a2
on a1. Student id = a2. Student id
) as a3
group by class id, Student id
) as a4
) as a5
where Class ranking <= 10
and Student id not in (
select distinct Student id
from Disciplinary table
where Time of violation >= date_sub(curdate(),interval 182 day)
);
【 The test point of this question 】
1) Check your understanding of date functions ;
2) Check the grouping function group by Flexible use of ;
3) Check your understanding of multi table connections , Especially flexible use where Condition to filter data .


️ Click on 「 Read the original 」
Free registration Data analysis training camp
边栏推荐
- 2022质量员-市政方向-岗位技能(质量员)考试模拟100题及模拟考试
- Alibaba Cloud 3 (Soaring Falcon) x86_64(Py3.7.8) 系统 YUM源
- [team learning] task06:for, if, and while
- 2022.2.28 variable length sequence table
- In depth research and analysis report on global and Chinese content audit market
- 2022-2028 global and Chinese near field scanning optical microscope (NSOM) market status and future development trend
- PowerShell主架构师:我用业余时间开发项目,表现优秀反而被微软降级了
- 树莓派知识大扫盲
- 树莓派获得网络安装系统功能,无需借助其他设备
- SQL must know and know
猜你喜欢

Individual income tax rate table

线程池的七个参数与拒绝策略

Live800: several ways for intelligent customer service to improve customer experience

mysql创建表出错1067 - Invalid default value for ‘update_time‘

Recommended open source scheduling libraries worth learning

What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger

Distributed file system and enterprise application -- elk enterprise log analysis system

.NET C#基础(6):命名空间 - 有名字的作用域

你违规了吗?

【Try to Hack】URL
随机推荐
JSTL custom label
Airtest automated test
In depth research and analysis report on ready to eat meat market for vacuum low temperature cooking in the world and China
I have something to say about final, finally and finalize
Which exchange is PTA futures on? How can PTA futures be safe?
2022 Hunan Provincial Safety officer-c certificate examination practice questions and online simulation examination
In depth research and analysis report on global and Chinese p-chlorotrifluoromethane Market
In depth research and analysis report on global and Chinese sanitary safety product market
小米9线刷ROM
SQL数据查询之单表查询
Seven parameters of thread pool and reject policy
In depth research and analysis report on global and Chinese smart lamp Market
2022-2028 China metal products market status research analysis and development prospect forecast report
Container -- reverse content -- use of explosion, splicing, and inversion functions
Ali, tell me about the application scenarios of message oriented middleware?
2022质量员-市政方向-岗位技能(质量员)考试模拟100题及模拟考试
Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
In depth research and analysis report on global and Chinese SURFBOARD WAX Market
2021-2027 China scaffold and accessories market status analysis and development prospect forecast report
IC fresh Chinese cabbage price of 400000 yuan! Experienced experts who have worked for many years guide you how to choose an offer!