当前位置:网站首页>Autumn move script B

Autumn move script B

2022-06-23 01:22:00 Shallow look

Exercise one : Transfer line column

hypothesis A B C The final exam results of the three children are shown below :
Create a student transcript student_score:

create table student_score
(name varchar(4) not null,
 subject varchar(64) not null,
 score integer(4) not null,
 primary key(name,subject));
 
insert into student_score values('A','chinese',99);
insert into student_score values('A','math',98);
insert into student_score values('A','english',97);
insert into student_score values('B','chinese',92);
insert into student_score values('B','math',91);
insert into student_score values('B','english',90);
insert into student_score values('C','chinese',88);
insert into student_score values('C','math',87);
insert into student_score values('C','english',86);

Please use SQL The code converts the above results into the following format ( Row to column ):

Their thinking :
Use Aggregate functions sum+case sentence

  1. adopt group by Statement by student name (name) Grouping
  2. adopt case Statement to query students separately chinese]、math、english Results of three courses
select name, 
			 sum(case when subject = 'chinese' then score else null end)as chinese,
			 sum(case when subject = 'math' then score else null end)as math,
			 sum(case when subject = 'english' then score else null end)as english
from student_score
group by name

 Insert picture description here

Exercise 2 : Column turned

hypothesis A B C The final exam results of the three children are shown below :
Create student score tables student_score2:

create table student_score2
(name varchar(4) not null,
 chinese integer(4) not null,
 math integer(4) not null,
 english integer(4) not null,
 primary key(name));

insert into student_score2 values('A',99,98,97);
insert into student_score2 values('B',92,91,90);
insert into student_score2 values('C',88,87,86);

Please use SQL The code converts the above results into the following format :

Their thinking 1:
Intuitive ideas mimic row to column , Use aggregate functions to add case sentence :

  1. No, subject, Therefore, according to the course name, it is defined as subject, And query separately chinese、math、english
  2. Join the set of queries , And according to name Sort to get column to row

Their thinking 2:
Use UNPIVOT, Refer to the article for specific ideas :SQL Transfer line column , Column turned
But I didn't run successfully , There will be time to sort out the reasons later

Their thinking 1 SQL sentence :

select name,
			 'chinese' as subject,
			 chinese as score
from student_score2
group by name
union
select name,
			 'math' as subject,
			 math as score
from student_score2
group by name
union
select name,
			'english' as subject,
			english as score
from student_score2
group by name
order by name			 

 Insert picture description here

Practice three : Anchor with goods

hypothesis , A platform 2021 The daily statistics of the annual anchor's sales are as follows :
Create sales statistics anchor_sales:

create table anchor_sales
(anchor_name varchar(4) not null,
 date integer(32) not null,
 sales integer(64) not null,
 primary key(anchor_name,date));

insert into anchor_sales values('A',20210101,40000);
insert into anchor_sales values('B',20210101,80000);
insert into anchor_sales values('A',20210102,10000);
insert into anchor_sales values('C',20210102,90000);
insert into anchor_sales values('A',20210103,7500);
insert into anchor_sales values('C',20210103,80000);

Definition : If the proportion of a certain day's sales of an anchor reaches... Of the total sales of the platform on that day 90% And above , The anchor is called the star anchor , That day is also called star anchor day .
Please use SQL Complete the following calculation :

a. 2021 How many star anchor days are there in ?

b. 2021 How many star anchors are there in ?

Their thinking :

  1. To count star anchors and dates , Therefore, it is grouped by date and host name
  2. adopt sum The function is easy to count the number of... Grouped by date and host name sales
  3. Through aggregate functions sum Use under window function , Count the total daily sales of the platform
  4. step 2 and 3 Divide , Get the percentage of the total sales of each anchor in the total sales of the platform on that day sales_rate
  5. Use where Select the star anchor and star anchor day that meet the conditions
    The proportion of daily sales of each anchor on the computing platform , And sort SQL sentence :
select date,anchor_name,
			(sum(sales)/sum(sales) over (partition by date))as sales_rate
from anchor_sales
group by date,anchor_name
order by date,sales_rate desc

Running results :
 Insert picture description here
The proportion of selected sales exceeds 90% Star anchor and star anchor day SQL sentence :

select *
from(	select date,anchor_name,
			(sum(sales)/sum(sales) over (partition by date))as sales_rate
			from anchor_sales
			group by date,anchor_name
			order by date,sales_rate desc)as p1
where sales_rate >= 0.9

 Insert picture description here

Exercise four :MySQL How to view sql Statement execution plan ? What information can you see ?

MySQL To view the execution plan, you only need to add... Before the query statement explain that will do , As an example , View the execution plan sql The statement is as follows :

explain select date,anchor_name,
			(sum(sales)/sum(sales) over (partition by date))as sales_rate
from anchor_sales
group by date,anchor_name
order by date,sales_rate desc

The query results are as follows :
 Insert picture description here
Detailed explanation of reference articles :MySQL EXPLAIN command : View query execution plan

Practice five : Explain it. SQL In the database ACID What is it?

  • Atomicity : Statements are either executed , Or it's not execution , Is the core feature of transactions , Transactions themselves are historically defined in terms of atomicity , The implementation is mainly based on undo log
  • persistence : Ensure that after the transaction is committed , It will not cause data loss due to downtime and other reasons , Mainly based on redo log Realization
  • Isolation, : Ensure that transactions are isolated from each other , The execution of a transaction is not affected by other transactions .InnoDB The default database isolation level of the storage engine is RR,RR It is mainly based on the lock mechanism , Hidden columns of data ,undo log class as well as next-key lock Mechanism
  • Uniformity : The ultimate goal of a transaction , The realization of consistency requires the guarantee of database level , Also needs the application layer safeguard .
    Details refer to :MySQL Business of ACID Realization principle ( Read... In all directions )

In this paper, the reference :
MySQL EXPLAIN command : View query execution plan
MySQL Business of ACID Realization principle ( Read... In all directions )

The full title of this article :
DataWhale Team learning

原网站

版权声明
本文为[Shallow look]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220918142891.html