MySQL Window Functions

本文深入讲解MySQL窗口函数的使用,自8.0版本起,MySQL引入了窗口函数,极大简化了解决复杂分析查询的难度,提高了查询效率。通过具体实例,如销售数据表,展示如何运用窗口函数进行数据汇总而不减少返回行数,与聚合函数和GROUP BY子句的对比,以及窗口函数的语法结构和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:MySQL Window Functions

Summary: in this tutorial, you will learn about the MySQL window functions and their useful applications in solving analytical query challenges.

MySQL has supported window functions since version 8.0. The window functions allow you to solve query problems in new, easier ways, and with better performance.

Suppose, we have a sales table that stores the sales by employees and fiscal years as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

CREATE TABLE sales(

    sales_employee VARCHAR(50) NOT NULL,

    fiscal_year INT NOT NULL,

    sale DECIMAL(14,2) NOT NULL,

    PRIMARY KEY(sales_employee,fiscal_year)

);

 

INSERT INTO sales(sales_employee,fiscal_year,sale)

VALUES('Bob',2016,100),

      ('Bob',2017,150),

      ('Bob',2018,200),

      ('Alice',2016,150),

      ('Alice',2017,100),

      ('Alice',2018,200),

       ('John',2016,200),

      ('John',2017,150),

      ('John',2018,250);

 

SELECT

    *

FROM

    sales

It’s probably easier to understand window functions is to start with aggregate functions.

Aggregate functions summarize data from multiple rows into a single result row. For example, the following SUM()function returns the total sales of all employees in the recorded years:

1

2

3

4

SELECT

    SUM(sale)

FROM

    sales;        

MySQL Window Function - vs Aggregate SUM

The GROUP BY clause allows you to apply aggregate functions to a subset of rows. For example, you may want to calculate the total sales by fiscal years:

1

2

3

4

5

6

7

SELECT

    fiscal_year,

    SUM(sale)

FROM

    sales

GROUP BY

    fiscal_year;  

MySQL Window Function - vs SUM with GROUP BY

In both examples, the aggregate functions reduce the number of rows returned by the query.

Like the aggregate functions with the GROUP BY clause, window functions also operate on a subset of rows but they do not reduce the number of rows returned by the query.

For example, the following query returns the sales for each employee, along with total sales of the employees by fiscal year:

1

2

3

4

5

6

7

SELECT

    fiscal_year,

    sales_employee,

    sale,

    SUM(sale) OVER (PARTITION BY fiscal_year) total_sales

FROM

    sales;

MySQL Window Function - SUM window function

In this example, the SUM() function works as a window function which operates on a set of rows defined by the contents of the OVER clause. A set of rows to which the SUM() function applies is referred to as a window.

The SUM() window function reports not only the total sales by fiscal year like it does in the query with the GROUP BY clause, but also the result in each row, rather than the total number of rows returned.

Note that window functions are performed on the result set after all JOINWHEREGROUP BY, and HAVING clauses and before the ORDER BYLIMIT and SELECT DISTINCT.

Window function syntax

The general syntax of calling a window function is as follows:

1

2

3

4

5

6

window_function_name(expression)

    OVER (

        [partition_defintion]

        [order_definition]

        [frame_definition]

    )

In this syntax:

  • First, specify the window function name followed by an expression.
  • Second, specify the OVER clause which has three possible elements: partition definition, order definition, and frame definition.

The opening and closing parentheses after the OVER clause are mandatory, even with no expression, for example:

1

window_function_name(expression) OVER()

 

partition_clause syntax

The partition_clause breaks up the rows into chunks or partitions. Two partitions are separated by a partition boundary.

The window function is performed within partitions and re-initialized when crossing the partition boundary.

The partition_clause syntax looks like the following:

1

PARTITION BY <expression>[{,<expression>...}]

You can specify one or more expressions in the PARTITION BY clause. Multiple expressions are separated by commas.

order_by_clause syntax

The order_by_clause has the following syntax:

1

ORDER BY <expression> [ASC|DESC], [{,<expression>...}]

The ORDER BY clause specifies how the rows are ordered within a partition. It is possible to order data within a partition on multiple keys, each key is specified by an expression. Multiple expressions are also separated by commas.

Similar to the PARTITION BY clause, the ORDER BY clause is also supported by all the window functions. However, it only makes sense to use the ORDER BY clause for order-sensitive window functions.

frame_clause syntax

A frame is a subset of the current partition. To define the subset, you use the frame clause as follows:

1

frame_unit {<frame_start>|<frame_between>}

A frame is defined with respect to the current row, which allows a frame to move within a partition depending on the position of the current row within its partition.

The frame unit specifies the type of relationship between the current row and frame rows. It can be ROWS or RANGE. The offsets of the current row and frame rows are the row numbers if the frame unit is ROWS and row values the frame unit is RANGE.

The frame_start and frame_between define the frame boundary.

The frame_start contains one of the following:

  • UNBOUNDED PRECEDING: frame starts at the first row of the partition.
  • N PRECEDING: a physical N of rows before the first current row. N can be a literal number or an expression that evaluates to a number.
  • CURRENT ROW: the row of the current calculation

The frame_between is as follows:

1

BETWEEN frame_boundary_1 AND frame_boundary_2  

The frame_boundary_1 and frame_boundary_2 can each contain one of the following:

  • frame_start: as mentioned previously.
  • UNBOUNDED FOLLOWING: the frame ends at the final row in the partition.
  • N FOLLOWING: a physical N of rows after the current row.

If you don’t specify the frame_definition in the OVER clause, then MySQL uses the following frame by default:

1

RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

mysql window functions - frame clause bound

MySQL Window Function list

The following table shows the window functions in MySQL:

NameDescription
CUME_DISTCalculates the cumulative distribution of a value in a set of values.
DENSE_RANKAssigns a rank to every row within its partition based on the ORDER BY clause. It assigns the same rank to the rows with equal values. If two or more rows have the same rank, then there will be no gaps in the sequence of ranked values.
FIRST_VALUEReturns the value of the specified expression with respect to the first row in the window frame.
LAGReturns the value of the Nth row before the current row in a partition. It returns NULL if no preceding row exists.
LAST_VALUEReturns the value of the specified expression with respect to the last row in the window frame.
LEADReturns the value of the Nth row after the current row in a partition. It returns NULL if no subsequent row exists.
NTH_VALUEReturns value of argument from Nth row of the window frame
NTILEDistributes the rows for each window partition into a specified number of ranked groups.
PERCENT_RANKCalculates the percentile rank of a row in a partition or result set
RANKSimilar to the DENSE_RANK() function except that there are gaps in the sequence of ranked values when two or more rows have the same rank.
ROW_NUMBERAssigns a sequential integer to every row within its partition

In this tutorial, you have learned about the MySQL window functions and their syntax. In the next tutorials, you will learn more about each window function and its applications in more detail.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值