当前位置:网站首页>MySQL functions and related cases and exercises
MySQL functions and related cases and exercises
2022-07-03 13:19:00 【Levi Bebe】
1.MySQL The main function of
1.1 Aggregate functions
group_concat() Use of functions
CREATE DATABASE bianbian4;
USE bianbian4;
CREATE TABLE emp (
emp_id INT PRIMARY KEY auto_increment COMMENT ' Number ',
emp_name VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT ' full name ',
salary DECIMAL ( 10, 2 ) NOT NULL DEFAULT 0 COMMENT ' Wages ',
department VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT ' department '
);
INSERT INTO emp ( emp_name, salary, department )
VALUES
( ' Zhang Jingjing ', 5000, ' Finance Department ' ),
( ' Wang Feifei ', 5800, ' Finance Department ' ),
( ' Zhao Gang ', 6200, ' Finance Department ' ),
( ' Liu Xiaobei ', 5700, ' The personnel department ' ),
( ' Wang Dapeng ', 6700, ' The personnel department ' ),
( ' Zhang Xiaopei ', 5200, ' The personnel department ' ),
( ' Liu yunyun ', 7500, ' The sales department ' ),
( ' Liu Yunpeng ', 7200, ' The sales department ' ),
( ' Liu Yunpeng ', 7800, ' The sales department ' );
-- Merge the names of all employees into one line
SELECT GROUP_CONCAT(emp_name) FROM emp;
-- Specify separator merge
SELECT GROUP_CONCAT(emp_name SEPARATOR ';') FROM emp;
-- Specify sorting method and separator
SELECT department,GROUP_CONCAT(emp_name ORDER BY salary desc SEPARATOR ';') FROM emp GROUP BY department;
1.2 Mathematical functions
-- Mathematical functions
SELECT abs(-10);
SELECT abs(10);
SELECT abs( Expression or field ) FROM surface ;
-- ceil Upward evidence collection ;
select ceil(1.1); -- 2
select ceil(1.0); -- 1
-- Rounding down
select floor(1.1); -- 1
select floor(1.9); -- 1
-- Taking the maximum
select greatest(1,2); -- 2
-- minimum value
select least(1,2,3); -- 1
1.3 String function
1.4 Date function
1.5 Control flow function
1.5.1 if Logical judgment statement
When judging the status , have access to IF Function to judge :
1.5.2 case when function
case when amount to C Linguistic switch() sentence
1.6 Window function
Window function classification :
Window function syntax structure :
1.6.1 Ordinal function
Ordinal function :row_number()、rank()、dense_rank(), Can be used to achieve grouping sorting , And add serial number .
use bianbian4;
create table employee(
dname varchar(20),
eid varchar(20),
ename varchar(20),
hiredate date,
salary double
);
-- The data class contains
insert into
employee
values
(' R & D department ','1001',' Liu bei ','2021-11-01',3000),
(' R & D department ','1002',' Guan yu ','2021-11-02',5000),
(' R & D department ','1003',' Zhang Fei ','2021-11-03',7000),
(' R & D department ','1004',' zhaoyun ','2021-11-04',7000),
(' R & D department ','1005',' d ','2021-11-05',4000),
(' R & D department ','1006',' Huang Zhong ','2021-11-06',4000),
(' The sales department ','1001',' Cao Cao ','2021-11-01',2000),
(' The sales department ','1002',' Xu Chu ','2021-11-02',3000),
(' The sales department ','1003',' Dianwei ','2021-11-03',5000),
(' The sales department ','1004',' Zhang liao ','2021-11-04',6000),
(' The sales department ','1005',' Xu Huang ','2021-11-05',9000),
(' The sales department ','1006',' Cao Hong ','2021-11-06',6000);
-- Sort the employees of each department according to their salary , And give the ranking
select dname,ename,salary,
row_number() over (partition by dname order by salary desc) as row_rank,
rank() over (partition by dname order by salary desc) as rk,
dense_rank() over (partition by dname order by salary desc) as dense_rk
from employee;
-- Find out the top three employees in each department ,- Grouping calculation topN
select *
from
(
select dname,ename,salary,
dense_rank() over (partition by dname order by salary desc) as dense_rk
from employee
) as t
where t.dense_rk<=3;
-- Sort all employees globally
select dname,ename,salary,
dense_rank() over (order by salary desc) as dense_rk
from employee
row_number(): Don't consider the differences , The numbers in the order do not change ,1,2,3
rank(): Consider the same situation , Skip number
dense_rank(): Consider the same situation , Don't jump numbers
Here's the picture :
1.6.2 Windowed aggregate function
-- Accumulate according to the grouping date
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate ) as c1
from employee
-- without order by Add all the data in the Group
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname ) as c1
from employee
Realize interval statistics , With sum As an example , The data is the data created above :
-- Specify the range to add
-- Start line : unbounded preceding To the current line current row
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between unbounded preceding and current row) as c1
from employee
-- The top three lines of the current line
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between 3 preceding and current row) as c1
from employee
-- Three lines up + Current row + Next line
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between 3 preceding and 1 following) as c1
from employee
-- Current line to the end
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between current row and unbounded following) as c1
from employee
1.6.3 Distribution function
cume_dist() Introduction to :
purpose : Less than... In the group 、 Equal to current rank The number of rows for the value / The total number of rows in the group
Application scenarios : Query the comparison column less than or equal to the current salary
cume_dist() Example :
use bianbian4;
select
dname,
ename,
salary,
cume_dist() over(order by salary) as rn1,
cume_dist() over(partition by dname order by salary) as rn2
from employee;
/* No addition partition by Is the total number of lines : rn1 It indicates the proportion of the whole salary less than or equal to the current number 3/12 = 0.25 (3000) 5/12 = 0.4166666666666667 (4000) Yes partition by, Look at this group : rn2 Meaning case : 1 / 6 = 0.16666666666666666 (3000, In the R & D group ) */
cume_dist() Result :
percent_rank Introduction to :
percent_rank Code :
select
dname,
ename,
salary,
rank() over(partition by dname order by salary desc) as rn1,
percent_rank() over(partition by dname order by salary desc) as rn2
from employee;
/* rn2: first line :(1-1)/ (6-1) = 0 The second line :(1-1)/ (6-1) = 0 The third line :(3-1)/ (6-1) = 0.4 */
percent_rank() result :
1.6.4 Before and after function LAG and LEAD
select
dname,
ename,
salary,
hiredate,
-- Put the... In the previous line , Put it behind the current , If there is no default '2000-01-01'
lag(hiredate,1,'2000-01-01') over(partition by dname order by hiredate ) as rn1,
-- Put the values of the last two lines , Put it behind the current , If there is no default null
lag(hiredate,2) over(partition by dname order by hiredate ) as rn2
from employee;
1.6.5 Head tail function first_value and last_value
first_value() and last_value() Case study :
select
dname,
ename,
salary,
hiredate,
-- The first one so far
first_value(salary) over(partition by dname order by hiredate ) as first_,
-- The last one so far
last_value(salary) over(partition by dname order by hiredate ) as last_
from employee;
1.6.6 Other functions nth_value(expr,n)、ntile(n)
nth_value(expr,n) Case study :
select
dname,
ename,
salary,
hiredate,
-- No. so far 1 Salary of the line
nth_value(salary,1) over(partition by dname order by hiredate ) as first_salary,
-- No. so far 2 Salary of the line
nth_value(salary,2) over(partition by dname order by hiredate ) as second_salary
from employee;
ntile Case study :
select
dname,
ename,
salary,
hiredate,
-- Divide into 3 Group
ntile(3) over(partition by dname order by hiredate ) as nt
from employee;
-- Take out the first group of employees in each department
select *
from
(
select
dname,
ename,
salary,
hiredate,
-- Divide into 3 Group
ntile(3) over(partition by dname order by hiredate ) as nt
from employee
) t
where t.nt =1;
Reference resources
https://www.bilibili.com/video/BV1iF411z7Pu?p=99&spm_id_from=pageDriver
边栏推荐
- C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
- MySQL constraints
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
- 2022-01-27 research on the minimum number of redis partitions
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- Sitescms v3.0.2 release, upgrade jfinal and other dependencies
- 剑指 Offer 15. 二进制中1的个数
- Kotlin - improved decorator mode
- elk笔记24--用gohangout替代logstash消费日志
- Introduction to the implementation principle of rxjs observable filter operator
猜你喜欢
File uploading and email sending
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
Dojo tutorials:getting started with deferrals source code and example execution summary
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
MySQL constraints
DQL basic query
JSP and filter
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
正则表达式
用户和组命令练习
随机推荐
【R】【密度聚类、层次聚类、期望最大化聚类】
Task6: using transformer for emotion analysis
【Colab】【使用外部数据的7种方法】
Seven habits of highly effective people
关于CPU缓冲行的理解
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
高效能人士的七个习惯
Sword finger offer 14- I. cut rope
Smbms project
A large select drop-down box, village in Chaoyang District
Introduction to the implementation principle of rxjs observable filter operator
18W word Flink SQL God Road manual, born in the sky
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
我的创作纪念日:五周年
File uploading and email sending
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Finite State Machine FSM
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271