Window functions in mysql

Window Functions in MySQL are functions used to perform calculations within query result sets. Window functions can perform analysis and aggregation operations in queries without grouping query results. They can be used to calculate various analytical operations such as rankings, row numbers, cumulative values, etc. Window functions are often used with the OVER clause to specify the extent of the window.

Common window functions include:

ROW_NUMBER(): Assigns a unique integer value to each row in the result set, indicating the order of the rows.
RANK(): Calculate the rank of each row, the same value will have the same rank, but there may be skipped ranks.
DENSE_RANK(): Calculate the rank of each row, the same value will have the same rank, but there is no skipped rank.
NTILE(n): Divide the result set into n approximately equal parts and assign a bucket number to each row.
SUM(), AVG(), COUNT(), MAX(), MIN(): Calculate the aggregated value of a column within the window range.
The following is an example query showing how to use window functions in MySQL:

SELECT
    employee_id,
    salary,
    RANK() OVER (ORDER BY salary DESC) AS rank
FROM
    employees;

In this query, the RANK() function is used to calculate the salary rank of each employee, sorted in descending order of salary.

Note that the use of window functions may involve specific syntax and support levels of different database management systems. Make sure your MySQL version supports window functions, and consult the official documentation for exact syntax and usage information when using them.

Niu Ke:
https://www.nowcoder.com/practice/8d2c290cc4e24403b98ca82ce45d04db?tpId=82&tqId=29762&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab %3DSQL%25E7% 25AF%2587%26topicId%3D240&difficulty=undefined&judgeStatus=undefined&tags=&title=

#################################################窗口函数测试
# drop table if exists  `salaries` ;
# CREATE TABLE `salaries` (
# `emp_no` int(11) NOT NULL,
# `salary` int(11) NOT NULL,
# `from_date` date NOT NULL,
# `to_date` date NOT NULL,
# PRIMARY KEY (`emp_no`,`from_date`));
# INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
# INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
# INSERT INTO salaries VALUES(10004,72527,'2002-08-02','9999-01-01');
# INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');
#窗口函数1
# select emp_no,salary,rank() over(order by salary desc) t
# from salaries
# 10001|88958|1
# 10002|72527|2
# 10004|72527|2
# 10003|43311|4
#RANK():计算每一行的排名,相同值会有相同的排名,但可能会跳过排名。
# select emp_no,salary
# from
# (select emp_no,salary,rank() over(order by salary desc) t
# from salaries) a
# where t=2
# 10002|72527
# 10004|72527
#窗口函数2
# select emp_no,salary,row_number() over(order by salary desc) t
# from salaries
# 10001|88958|1
# 10002|72527|2
# 10004|72527|3
# 10003|43311|4
#ROW_NUMBER():为结果集中的每一行分配唯一的整数值,表示行的顺序。
# select emp_no,salary
# from
# (select emp_no,salary,row_number() over(order by salary desc) t
# from salaries) a
# where t=2
# 10002|72527
#窗口函数3
# select emp_no,salary,DENSE_RANK() over(order by salary desc) t
# from salaries
# 10001|88958|1
# 10002|72527|2
# 10004|72527|2
# 10003|43311|3
#DENSE_RANK():计算每一行的排名,相同值会有相同的排名,但不会跳过排名。
# select emp_no,salary
# from
# (select emp_no,salary,DENSE_RANK() over(order by salary desc) t
# from salaries) a
# where t=2
# 10002|72527
# 10004|72527
#窗口函数4 
# select emp_no,salary,NTILE(4) over(order by salary desc) t
# from salaries
# 10001|88958|1
# 10002|72527|2
# 10004|72527|3
# 10003|43311|4

#窗口函数5
#SUM(), AVG(), COUNT(), MAX(), MIN(): 在窗口范围内计算某一列的聚合值。
select emp_no,salary,SUM(salary) over(PARTITION BY emp_no) t
from salaries

chatgpt:
The use of NTILE
insert image description here
The above is the use of table structure information
insert image description here
SUM, and other functions.
insert image description here
The above is the table structure information

insert image description here
Added content:
(For those who don’t understand, you can take a look at:
row_number(): same salary but different name, which is equivalent to row number, for example, 3000, 2000, 2000, 1000 ranked as 1, 2, 3, 4
rank(): same The same salary and name, there is a skip level, for example, 1, 2, 2, 4 after the ranking of 3000, 2000, 2000, 1000
dense_rank(): the same salary and the same name, no skipping, such as 3000, 2000, 2000, 1000 after the ranking is 1, 2, 4 2, 3
ntile(): Bucket ranking, that is, first divide the first, second and third buckets according to the number of buckets, and then rank from 1 in each bucket. In fact, partition by is not very commonly used, and order by is divided according to a certain field
.
The regular order by is used in the same way, and ASC (default) and DESC are also distinguished, because there must always be a basis for ranking

Author: luanhz
Link: https://leetcode-cn.com/problems/nth-highest-salary/solution/mysql-zi-ding-yi-bian-liang-by-luanz/

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/132382671
Recommended