当前位置:网站首页>Autumn move script B
Autumn move script B
2022-06-23 01:22:00 【Shallow look】
List of articles
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
- adopt group by Statement by student name (name) Grouping
- 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

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 :
- No, subject, Therefore, according to the course name, it is defined as subject, And query separately chinese、math、english
- 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

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 :
- To count star anchors and dates , Therefore, it is grouped by date and host name
- adopt sum The function is easy to count the number of... Grouped by date and host name sales
- Through aggregate functions sum Use under window function , Count the total daily sales of the platform
- 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
- 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 :
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

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 :
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
边栏推荐
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- OOP multiple storage (class template)
- How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
- Is it safe to invest in funds through daily funds? I intend to open an account to buy funds
- Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc
- Vector 3 (static member)
- 层次选择器
- Overview of visual object detection technology based on deep learning
- How to solve the problem that easycvr does not display the interface when RTMP streaming is used?
- SYSTEMd summary
猜你喜欢

LeetCode 206. 反转链表(迭代+递归)

E-R图

LeetCode 206. Reverse linked list (iteration + recursion)
![[initial launch] there are too many requests at once, and the database is in danger](/img/c1/807575e1340b8f8fe54197720ef575.png)
[initial launch] there are too many requests at once, and the database is in danger

Dig three feet to solve the data consistency problem between redis and MySQL

E-R diagram
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

Steps to implement a container global component

數據庫中數據的儲存結構和方式是什麼?
Yyds dry goods counting tail recursion is better than recursion
随机推荐
The road of architects starts from "storage selection"
一文读懂基于Redis的Amazon MemoryDB数据库
Big guys, 2.2.1 the CDC monitoring SQLSEVER can only get the full amount of data. Why can't we get the incremental data in the later period
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
Yyds dry goods counting tail recursion is better than recursion
Overview of visual object detection technology based on deep learning
LINQ 查询
Tidb monitoring upgrade: a long way to solve panic
JS to determine whether the browser has opened the console
OSPF experiment in mGRE environment
Add / get and delete cookies
数据库中数据的储存结构和方式是什么?
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
JMeter associated login 302 type interface
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
Voice network multiplayer video recording and synthesis support offline re recording | Nuggets technology solicitation
JS to read the picture of the clipboard
Vector 2 (friend and copy construction)
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
LINQ 查詢