MySQL uses window functions ROW_NUMBER() and DENSE_RANK() to query the first place in each group or the top few in each group. Detailed explanation of the use of window functions.

MySQL data table structure

Create the tbl_class_info table, which has four fields: id, username, score, group_name

Use ROW_NUMBER(), DENSE_RANK() to query The top three in each group

-- 查询每组前3名
SELECT username, score, group_name  
FROM (  
  SELECT username, score, group_name,  
         ROW_NUMBER() OVER (PARTITION BY group_name ORDER BY score DESC) AS test_rank,
         DENSE_RANK() OVER (PARTITION BY group_name ORDER BY score DESC) AS test_dense_rank  
  FROM tbl_class_info 
) AS ranked_scores  
WHERE test_rank <= 3 OR test_dense_rank <= 3

search result:

Use ROW_NUMBER(), DENSE_RANK() to querySecond place

-- 查询每组第2名
SELECT username, score, group_name  
FROM (  
  SELECT username, score, group_name,  
         ROW_NUMBER() OVER (PARTITION BY group_name ORDER BY score DESC) AS test_rank 
  FROM tbl_class_info 
) AS ranked_scores  
WHERE test_rank = 2

search result:

The query results using ROW_NUMBER() and DENSE_RANK() are different.

ROW_NUMBER() and DENSE_RANK() are common window functions that can be used to sort and number the rows in the result set. Their main difference is that  Use the DENSE_RANK() function to sort the records in each group (group_name) in descending order according to their grades (score), and assign a unique sequence number (rank) to the rows in each group.

Unlike ROW_NUMBER(),If the same grades exist, DENSE_RANK() will assign them consecutive serial numbers without skipping any numbers. Here is an example:

Detailed explanation of the differences between ROW_NUMBER() and DENSE_RANK()

Use ROW_NUMBER() to query data:

SELECT
	username,
	score,
	group_name,
	ROW_NUMBER() OVER (PARTITION BY group_name
ORDER BY
	score DESC) AS rank_number
FROM
	tbl_class_info;

You can see the execution results.Even if the scores are the same, the rank_number numbers are different

Use DENSE_RANK() to query data:

SELECT
	username,
	score,
	group_name,
	DENSE_RANK() OVER (PARTITION BY group_name
ORDER BY
	score DESC) AS rank_number
FROM
	tbl_class_info;

You can see the execution results.Even if the scores are the same, the rank_number serial numbers are the same

So in summary, ROW_NUMBER() and DENSE_RANK() are very useful window functions that can be used for various data analysis tasks. However, the differences between them mean they are suitable for different scenarios. If you need to assign a unique number to each row, even if there are duplicate values, then ROW_NUMBER() is a better choice. If you need to assign a unique number to each row, but need to skip duplicate values ​​if there are any, then DENSE_RANK() is a better choice.

MySQL window function usage syntax

SELECT <窗口函数> OVER (
  [PARTITION BY <表达式>]
  [ORDER BY <表达式>]
  [ROWS BETWEEN <表达式> AND <表达式>]
)
FROM <表名>

 in: 

  • <Window function> is the name of the window function.​ 
  • PARTITION BY <expression> is a partition expression. Partition expressions are used to divide rows in a table into multiple partitions.​ 
  • ORDER BY <expression> is a sorting expression. Sort expressions are used to sort rows in a partition.​ 
  • ROWS BETWEEN <expression> AND <expression> are row range expressions. The row range expression is used to specify the range of rows that the window function evaluates.
SELECT
	group_name,
	AVG(score) OVER (PARTITION BY group_name
ORDER BY
	score DESC ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS avg_sales
FROM
	tbl_class_info;

Results of the:

Note: In the window function, ROWS BETWEEN <expression> AND <expression> is used to specify the boundary range of the window. This range is determined based on the specified expression. Typically, the first expression specifies the starting line of the window, and the second expression specifies the ending line of the window.

Before calculating the window function, the database will first determine the scope of the window. Then, based on the specified window range, operations are performed on the rows within the range. Normally, window functions perform calculations on each row within the window and return a result corresponding to the window range.
 

What are the common window functions in MySQL?

  SUM() function: Calculates the sum of specified columns.

SELECT column1, SUM(column2) OVER (PARTITION BY column1) AS total_sum  
FROM table;

  AVG() function: Calculate the average of the specified column.

SELECT column1, AVG(column2) OVER (PARTITION BY column1) AS average_value  
FROM table;

  MIN() function: Returns the minimum value of the specified column.

SELECT column1, MIN(column2) OVER (PARTITION BY column1) AS minimum_value  
FROM table;

  MAX() function: Returns the maximum value of the specified column.

SELECT column1, MAX(column2) OVER (PARTITION BY column1) AS maximum_value  
FROM table;

  COUNT() function: Counts the number of non-null values ​​in a specified column.

SELECT column1, COUNT(column2) OVER (PARTITION BY column1) AS count_value  
FROM table;

  RANK() function: Returns the ranking of a set of rows.

SELECT column1, RANK() OVER (ORDER BY column2 DESC) AS rank_value  
FROM table;

  DENSE_RANK() function: Returns the uninterrupted ranking of a set of rows.

SELECT column1, DENSE_RANK() OVER (ORDER BY column2 DESC) AS dense_rank_value  
FROM table;

  ROW_NUMBER() function: assigns a unique number to a set of rows.

SELECT column1, ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column2 DESC) AS row_number_value  
FROM table;

The difference between MySQL window functions and aggregate functions

Window functions and aggregate functions are both functions that perform calculations on data in the database. However, there are some key differences between them.​ 

  • Window functions return a single value calculated based on a set of rows. This group of rows is called a window. Window functions can use all the rows in the window or some of the rows in the window.​ 
  • Aggregation functions return a single value that is calculated based on the data in the entire table. Aggregation functions cannot use windows because they require access to the entire table's data to calculate the result.​ 
SELECT SUM(sales) FROM orders;

SELECT SUM(sales) OVER (PARTITION BY product_id ORDER BY order_date);

The first query uses the aggregate function SUM to calculate the total sales for all orders in the entire table.
The second query uses the window function SUM to calculate the total sales for each product. The window function SUM uses the ORDER BY clause to sort orders by date and then calculates the total sales for each product. 
Window functions and aggregate functions are powerful tools for calculating data in the database. However, there are some key differences between them that you need to consider when choosing which function to use.

Guess you like

Origin blog.csdn.net/amosjob/article/details/134934538