当前位置:网站首页>Did you break the rules?

Did you break the rules?

2022-06-11 14:31:00 Monkey data analysis

b4d372c4772c4e3289f17c731f566076.png

【 Interview questions 】

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

8d94e5853539ea5cc011f32553465056.png

4977728fafa5239700519208eaf70588.png

4a1c7545e51cdf300a1b7e0efb9a6760.png

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 ”.

2c361f973579ff1d0aca8e8a1bd7c7e4.png

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 :

dabc5eccde182d3a82ba4268de989cfe.png

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 .

20ef6f5429fd2fc096a1c5da383b71e1.png

c69801e0fd808f6b8ffdc940e292f6a7.png

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 :

6265b10767c80bec2135b058a4c21c5b.png

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 :

c95d19d62867413dfd6c21d9acf11665.png

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)
);

a1c95d6cca1ce83c830baa16de189cf0.png

【 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 .

0dfa1749c8f48418a5a41b657b3b42ca.png

1d5ff093474e42d2588fe34c9f0e5c40.png

 ️  Click on 「 Read the original 」

  Free registration   Data analysis training camp

原网站

版权声明
本文为[Monkey data analysis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111409150754.html

随机推荐