This section describes nonaggregate window functions that, for each row from a query, perform a calculation using rows related to that row. 本节介绍非聚集窗口函数,对于查询中的每一行,使用与该行相关的行执行计算。Most aggregate functions also can be used as window functions; see Section 12.20.1, “Aggregate Function Descriptions”.大多数聚合函数也可用作窗口函数;请参阅第12.20.1节,“聚合功能描述”。
For window function usage information and examples, and definitions of terms such as the 有关窗口函数用法信息和示例,以及OVER
clause, window, partition, frame, and peer, see Section 12.21.2, “Window Function Concepts and Syntax”.OVER
子句、窗口、分区、框架和对等等术语的定义,请参阅第12.21.2节,“窗口函数概念和语法”。
Table 12.26 Window Functions窗口函数
Name | Description |
---|---|
CUME_DIST() | |
DENSE_RANK() | |
FIRST_VALUE() | |
LAG() | |
LAST_VALUE() | |
LEAD() | |
NTH_VALUE() | |
NTILE() | |
PERCENT_RANK() | |
RANK() | |
ROW_NUMBER() |
In the following function descriptions, 在下面的函数描述中,over_clause
represents the OVER
clause, described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
表示OVER
子句,如第12.21.2节,“窗口函数概念和语法”所述。Some window functions permit a 一些窗口函数允许null_treatment
clause that specifies how to handle NULL
values when calculating results. null_treatment
子句,指定在计算结果时如何处理NULL
值。This clause is optional. 此子句是可选的。It is part of the SQL standard, but the MySQL implementation permits only 它是SQL标准的一部分,但是MySQL实现只允许RESPECT NULLS
(which is also the default). RESPECT NULLS
(这也是默认值)。This means that 这意味着在计算结果时会考虑NULL
values are considered when calculating results. NULL
值。IGNORE NULLS
is parsed, but produces an error.IGNORE NULLS
会被解析,但会产生错误。
CUME_DIST()
over_clause
Returns the cumulative distribution of a value within a group of values; that is, the percentage of partition values less than or equal to the value in the current row. 返回值在一组值中的累积分布;即分区值小于或等于当前行中的值的百分比。This represents the number of rows preceding or peer with the current row in the window ordering of the window partition divided by the total number of rows in the window partition. Return values range from 0 to 1.这表示窗口分区的窗口顺序中与当前行相邻或对等的行数除以窗口分区中的总行数。返回值的范围从0到1。
This function should be used with 此函数应与ORDER BY
to sort partition rows into the desired order. ORDER BY
一起使用,以便按所需顺序对分区行进行排序。Without 如果没有ORDER BY
, all rows are peers and have value N
/N
= 1, where N
is the partition size.ORDER BY
,则所有行都是对等的,并且值N
/N
= 1,其中N
是分区大小。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。
The following query shows, for the set of values in the 对于val
column, the CUME_DIST()
value for each row, as well as the percentage rank value returned by the similar PERCENT_RANK()
function. val
列中的一组值,下面的查询显示每行的CUME_DIST()
值,以及类似的PERCENT_rank()
函数返回的百分比秩值。For reference, the query also displays row numbers using 作为参考,查询还使用ROW_NUMBER()
:ROW_NUMBER()
显示行号:
mysql>SELECT
val,
ROW_NUMBER() OVER w AS 'row_number',
CUME_DIST() OVER w AS 'cume_dist',
PERCENT_RANK() OVER w AS 'percent_rank'
FROM numbers
WINDOW w AS (ORDER BY val);
+------+------------+--------------------+--------------+ | val | row_number | cume_dist | percent_rank | +------+------------+--------------------+--------------+ | 1 | 1 | 0.2222222222222222 | 0 | | 1 | 2 | 0.2222222222222222 | 0 | | 2 | 3 | 0.3333333333333333 | 0.25 | | 3 | 4 | 0.6666666666666666 | 0.375 | | 3 | 5 | 0.6666666666666666 | 0.375 | | 3 | 6 | 0.6666666666666666 | 0.375 | | 4 | 7 | 0.8888888888888888 | 0.75 | | 4 | 8 | 0.8888888888888888 | 0.75 | | 5 | 9 | 1 | 1 | +------+------------+--------------------+--------------+
DENSE_RANK()
over_clause
Returns the rank of the current row within its partition, without gaps. 返回当前行在其分区内的排名,不带间距。Peers are considered ties and receive the same rank. This function assigns consecutive ranks to peer groups; the result is that groups of size greater than one do not produce noncontiguous rank numbers. 同龄人被认为是联系在一起的,并获得相同的排名。此函数将连续的列组分配给对等组;结果是大于1的组不会产生非连续的秩数。For an example, see the 有关示例,请参见RANK()
function description.RANK()
函数说明。
This function should be used with 此函数应与ORDER BY
to sort partition rows into the desired order. ORDER BY
一起使用,以便按所需顺序对分区行进行排序。Without 如果没有ORDER BY
, all rows are peers.ORDER BY
,则所有行都是对等行。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。
FIRST_VALUE(
[expr
)null_treatment
] over_clause
Returns the value of 从窗口框架的第一行返回expr
from the first row of the window frame.expr
的值。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
如第12.21.2节,“窗口函数概念和语法”所述。null_treatment
is as described in the section introduction.null_treatment
如引言部分所述。
The following query demonstrates 下面的查询演示FIRST_VALUE()
, LAST_VALUE()
, and two instances of NTH_VALUE()
:FIRST_VALUE()
、LAST_VALUE()
和NTH_VALUE()
的两个实例:
mysql>SELECT
time, subject, val,
FIRST_VALUE(val) OVER w AS 'first',
LAST_VALUE(val) OVER w AS 'last',
NTH_VALUE(val, 2) OVER w AS 'second',
NTH_VALUE(val, 4) OVER w AS 'fourth'
FROM observations
WINDOW w AS (PARTITION BY subject ORDER BY time
ROWS UNBOUNDED PRECEDING);
+----------+---------+------+-------+------+--------+--------+ | time | subject | val | first | last | second | fourth | +----------+---------+------+-------+------+--------+--------+ | 07:00:00 | st113 | 10 | 10 | 10 | NULL | NULL | | 07:15:00 | st113 | 9 | 10 | 9 | 9 | NULL | | 07:30:00 | st113 | 25 | 10 | 25 | 9 | NULL | | 07:45:00 | st113 | 20 | 10 | 20 | 9 | 20 | | 07:00:00 | xh458 | 0 | 0 | 0 | NULL | NULL | | 07:15:00 | xh458 | 10 | 0 | 10 | 10 | NULL | | 07:30:00 | xh458 | 5 | 0 | 5 | 10 | NULL | | 07:45:00 | xh458 | 30 | 0 | 30 | 10 | 30 | | 08:00:00 | xh458 | 25 | 0 | 25 | 10 | 30 | +----------+---------+------+-------+------+--------+--------+
Each function uses the rows in the current frame, which, per the window definition shown, extends from the first partition row to the current row. 每个函数都使用当前帧中的行,根据所示的窗口定义,这些行从第一个分区行扩展到当前行。For the 对于第n个NTH_VALUE()
calls, the current frame does not always include the requested row; in such cases, the return value is NULL
.VALUE()
调用,当前帧并不总是包含请求的行;在这种情况下,返回值为NULL
。
LAG(
[expr
[, N
[, default
]])null_treatment
] over_clause
Returns the value of 返回在当前行的分区内落后于当前行expr
from the row that lags (precedes) the current row by N
rows within its partition. N
行的行的expr
值。If there is no such row, the return value is 如果没有这样的行,则返回值为default
. default
。For example, if 例如,如果N
is 3, the return value is default
for the first two rows. N
是3,则返回值是前两行的default
。If 如果缺少N
or default
are missing, the defaults are 1 and NULL
, respectively.N
或default
,则默认值分别为1和NULL
。
N
must be a literal nonnegative integer. N
必须是文本非负整数。If 如果N
is 0, expr
is evaluated for the current row.N
为0,则对当前行计算expr
。
Beginning with MySQL 8.0.22, 从MySQL 8.0.22开始,N
cannot be NULL
. N
不能为空。In addition, it must now be an integer in the range 此外,它现在必须是1
to 263
, inclusive, in any of the following forms:1
到263
(含)范围内的整数,形式如下:
an unsigned integer constant literal无符号整数常量
a positional parameter marker (位置参数标记(?
)?
)
a user-defined variable用户定义的变量
a local variable in a stored routine存储例程中的局部变量
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
如第12.21.2节,“窗口函数概念和语法”所述。null_treatment
is as described in the section introduction.null_treatment
如引言部分所述。
LAG()
(and the similar LEAD()
function) are often used to compute differences between rows. LAG()
(和类似的LEAD()
函数)通常用于计算行之间的差异。The following query shows a set of time-ordered observations and, for each one, the 下面的查询显示了一组按时间顺序排列的观察值,对于每个观察值,还显示了相邻行的LAG()
and LEAD()
values from the adjoining rows, as well as the differences between the current and adjoining rows:LAG()
和LEAD()
值,以及当前行和相邻行之间的差异:
mysql>SELECT
t, val,
LAG(val) OVER w AS 'lag',
LEAD(val) OVER w AS 'lead',
val - LAG(val) OVER w AS 'lag diff',
val - LEAD(val) OVER w AS 'lead diff'
FROM series
WINDOW w AS (ORDER BY t);
+----------+------+------+------+----------+-----------+ | t | val | lag | lead | lag diff | lead diff | +----------+------+------+------+----------+-----------+ | 12:00:00 | 100 | NULL | 125 | NULL | -25 | | 13:00:00 | 125 | 100 | 132 | 25 | -7 | | 14:00:00 | 132 | 125 | 145 | 7 | -13 | | 15:00:00 | 145 | 132 | 140 | 13 | 5 | | 16:00:00 | 140 | 145 | 150 | -5 | -10 | | 17:00:00 | 150 | 140 | 200 | 10 | -50 | | 18:00:00 | 200 | 150 | NULL | 50 | NULL | +----------+------+------+------+----------+-----------+
In the example, the 在本例中,LAG()
and LEAD()
calls use the default N
and default
values of 1 and NULL
, respectively.LAG()
和LEAD()
调用分别使用默认的N和默认值1和NULL
。
The first row shows what happens when there is no previous row for 第一行显示LAG()
: The function returns the default
value (in this case, NULL
). LAG()
没有前一行时的情况:函数返回默认值(在本例中为NULL
)。The last row shows the same thing when there is no next row for 当LEAD()
.LEAD()
没有下一行时,最后一行显示相同的内容。
LAG()
and LEAD()
also serve to compute sums rather than differences. LAG()
和LEAD()
还用于计算和而不是差。Consider this data set, which contains the first few numbers of the Fibonacci series:考虑这个数据集,它包含斐波那契级数的前几个数:
mysql> SELECT n FROM fib ORDER BY n;
+------+
| n |
+------+
| 1 |
| 1 |
| 2 |
| 3 |
| 5 |
| 8 |
+------+
The following query shows the 下面的查询显示与当前行相邻的行的LAG()
and LEAD()
values for the rows adjacent to the current row. LAG()
和LEAD()
值。It also uses those functions to add to the current row value the values from the preceding and following rows. 它还使用这些函数将上一行和下一行的值添加到当前行值中。The effect is to generate the next number in the Fibonacci series, and the next number after that:其效果是生成斐波那契级数中的下一个数,以及之后的下一个数:
mysql>SELECT
n,
LAG(n, 1, 0) OVER w AS 'lag',
LEAD(n, 1, 0) OVER w AS 'lead',
n + LAG(n, 1, 0) OVER w AS 'next_n',
n + LEAD(n, 1, 0) OVER w AS 'next_next_n'
FROM fib
WINDOW w AS (ORDER BY n);
+------+------+------+--------+-------------+ | n | lag | lead | next_n | next_next_n | +------+------+------+--------+-------------+ | 1 | 0 | 1 | 1 | 2 | | 1 | 1 | 2 | 2 | 3 | | 2 | 1 | 3 | 3 | 5 | | 3 | 2 | 5 | 5 | 8 | | 5 | 3 | 8 | 8 | 13 | | 8 | 5 | 0 | 13 | 8 | +------+------+------+--------+-------------+
One way to generate the initial set of Fibonacci numbers is to use a recursive common table expression. 生成斐波那契数初始集的一种方法是使用递归公共表表达式。For an example, see Fibonacci Series Generation.有关示例,请参阅斐波那契级数生成。
Beginning with MySQL 8.0.22, you cannot use a negative value for the rows argument of this function.从MySQL8.0.22开始,这个函数的rows参数不能使用负值。
LAST_VALUE(
[expr
)null_treatment
] over_clause
Returns the value of 返回窗口框架最后一行的expr
from the last row of the window frame.expr
值。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
如第12.21.2节,“窗口函数概念和语法”所述。null_treatment
is as described in the section introduction.null_treatment
如引言部分所述。
For an example, see the 有关示例,请参见FIRST_VALUE()
function description.FIRST_VALUE()
函数说明。
LEAD(
[expr
[, N
[, default
]])null_treatment
] over_clause
Returns the value of 返回在分区内以expr
from the row that leads (follows) the current row by N
rows within its partition. N
行作为当前行的前导(后)行的expr
值。If there is no such row, the return value is 如果没有这样的行,则返回值为default
. default
。For example, if 例如,如果N
is 3, the return value is default
for the last two rows. N
为3,则最后两行的返回值为default
。If 如果缺少N
or default
are missing, the defaults are 1 and NULL
, respectively.N
或default
,则默认值分别为1和NULL
。
N
must be a literal nonnegative integer. N
必须是文本非负整数。If 如果N
is 0, expr
is evaluated for the current row.N
为0,则对当前行计算expr
。
Beginning with MySQL 8.0.22, 从MySQL 8.0.22开始,N
cannot be NULL
. N
不能为NULL
。In addition, it must now be an integer in the range 此外,它现在必须是1
to 263
, inclusive, in any of the following forms:1
到263
(含)范围内的整数,形式如下:
an unsigned integer constant literal无符号整数常量
a positional parameter marker (位置参数标记(?
)?
)
a user-defined variable用户定义的变量
a local variable in a stored routine存储例程中的局部变量
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
如第12.21.2节,“窗口函数概念和语法”所述。null_treatment
is as described in the section introduction.null_treatment
如引言部分所述。
For an example, see the 有关示例,请参见LAG()
function description.LAG()
函数说明。
In MySQL 8.0.22 and later, use of a negative value for the rows argument of this function is not permitted.在MySQL 8.0.22及更高版本中,不允许对该函数的rows参数使用负值。
NTH_VALUE(
[expr
, N
)from_first_last
] [null_treatment
] over_clause
Returns the value of 从窗口框架的第expr
from the N
-th row of the window frame. N
行返回expr
的值。If there is no such row, the return value is 如果没有这样的行,则返回值为NULL
.NULL
。
N
must be a literal positive integer.N
必须是文本正整数。
from_first_last
is part of the SQL standard, but the MySQL implementation permits only FROM FIRST
(which is also the default). from_first_last
是SQL标准的一部分,但是MySQL实现只允许FROM FIRST
(这也是默认值)。This means that calculations begin at the first row of the window. 这意味着计算从窗口的第一行开始。FROM LAST
is parsed, but produces an error. FROM LAST
被解析,但产生一个错误。To obtain the same effect as 要获得与FROM LAST
(begin calculations at the last row of the window), use ORDER BY
to sort in reverse order.FROM LAST
相同的效果(从窗口的最后一行开始计算),请使用ORDER BY
按相反顺序排序。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”. over_clause
如第12.21.2节,“窗口函数概念和语法”所述。null_treatment
is as described in the section introduction.null_treatment
如引言部分所述。
For an example, see the 有关示例,请参见FIRST_VALUE()
function description.FIRST_VALUE()
说明。
In MySQL 8.0.22 and later, you cannot use 在MySQL8.0.22及更高版本中,此函数的行参数不能使用NULL
for the row argument of this function.NULL
。
NTILE(
N
)over_clause
Divides a partition into 将分区划分为N
groups (buckets), assigns each row in the partition its bucket number, and returns the bucket number of the current row within its partition. N
个组(bucket),为分区中的每一行分配桶编号,并返回分区中当前行的桶编号。For example, if 例如,如果N
is 4, NTILE()
divides rows into four buckets. N
是4,那么NTILE()
将行划分为四个存储桶。If 如果N
is 100, NTILE()
divides rows into 100 buckets.N
是100,那么NTILE()
将行划分为100个桶。
N
must be a literal positive integer. N
必须是文本正整数。Bucket number return values range from 1 to 桶编号返回值的范围从1到N
.N
。
Beginning with MySQL 8.0.22, 从MySQL 8.0.22开始,N
cannot be NULL
. N
不能为空。In addition, it must be an integer in the range 此外,它必须是1
to 263
, inclusive, in any of the following forms:1
到263
(含)范围内的整数,形式如下:
an unsigned integer constant literal无符号整数常量
a positional parameter marker (位置参数标记(?
)?
)
a user-defined variable用户定义的变量
a local variable in a stored routine存储例程中的局部变量
This function should be used with 此函数应与ORDER BY
to sort partition rows into the desired order.ORDER BY
一起使用,以便按所需顺序对分区行进行排序。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。
The following query shows, for the set of values in the 下面的查询显示,对于val
column, the percentile values resulting from dividing the rows into two or four groups. val
列中的一组值,将行划分为两个或四个组所产生的百分位值。For reference, the query also displays row numbers using 作为参考,查询还使用ROW_NUMBER()
:ROW_NUMBER()
显示行号:
mysql>SELECT
val,
ROW_NUMBER() OVER w AS 'row_number',
NTILE(2) OVER w AS 'ntile2',
NTILE(4) OVER w AS 'ntile4'
FROM numbers
WINDOW w AS (ORDER BY val);
+------+------------+--------+--------+ | val | row_number | ntile2 | ntile4 | +------+------------+--------+--------+ | 1 | 1 | 1 | 1 | | 1 | 2 | 1 | 1 | | 2 | 3 | 1 | 1 | | 3 | 4 | 1 | 2 | | 3 | 5 | 1 | 2 | | 3 | 6 | 2 | 3 | | 4 | 7 | 2 | 3 | | 4 | 8 | 2 | 4 | | 5 | 9 | 2 | 4 | +------+------------+--------+--------+
Beginning with MySQL 8.0.22, the construct 从MySQL8.0.22开始,不再允许构造NTILE(NULL)
is no longer permitted.NTILE(NULL)
。
PERCENT_RANK()
over_clause
Returns the percentage of partition values less than the value in the current row, excluding the highest value. 返回小于当前行中的值(不包括最高值)的分区值的百分比。Return values range from 0 to 1 and represent the row relative rank, calculated as the result of this formula, where 返回值的范围从0到1,表示行的相对秩,根据此公式计算,其中rank
is the row rank and rows
is the number of partition rows:rank
是行秩,row
是分区行数:
(rank
- 1) / (rows
- 1)
This function should be used with 此函数应与ORDER BY
to sort partition rows into the desired order. ORDER BY
一起使用,以便按所需顺序对分区行进行排序。Without 如果没有ORDER BY
, all rows are peers.ORDER BY
,则所有行都是对等行。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。
For an example, see the 有关示例,请参见CUME_DIST()
function description.CUME_DIST()
函数说明。
RANK()
over_clause
Returns the rank of the current row within its partition, with gaps. 返回当前行在其分区内的排名(带间距)。Peers are considered ties and receive the same rank. 同龄人被认为是联系在一起的,并获得相同的排名。This function does not assign consecutive ranks to peer groups if groups of size greater than one exist; the result is noncontiguous rank numbers.如果存在大于一个的组,则此函数不会将连续的列组分配给对等组;结果是非连续的秩数。
This function should be used with 此函数应与ORDER BY
to sort partition rows into the desired order. ORDER BY
一起使用,以便按所需顺序对分区行进行排序。Without 如果没有ORDER BY
, all rows are peers.ORDER BY
,则所有行都是对等行。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。
The following query shows the difference between 下面的查询显示RANK()
, which produces ranks with gaps, and DENSE_RANK()
, which produces ranks without gaps. RANK()
和densed_RANK()
之间的差异,前者生成有间隙的列组,后者生成没有间隙的列组。The query shows rank values for each member of a set of values in the 查询显示val
column, which contains some duplicates. val
列中一组值的每个成员的秩值,其中包含一些重复项。RANK()
assigns peers (the duplicates) the same rank value, and the next greater value has a rank higher by the number of peers minus one. RANK()
为对等方(重复方)分配相同的秩值,下一个较大的值的秩高出对等方数量减1。DENSE_RANK()
also assigns peers the same rank value, but the next higher value has a rank one greater. DENSE_RANK()
还为对等方分配相同的秩值,但下一个较高的值的秩值较大。For reference, the query also displays row numbers using 作为参考,查询还使用ROW_NUMBER()
:ROW_NUMBER()
显示行号:
mysql>SELECT
val,
ROW_NUMBER() OVER w AS 'row_number',
RANK() OVER w AS 'rank',
DENSE_RANK() OVER w AS 'dense_rank'
FROM numbers
WINDOW w AS (ORDER BY val);
+------+------------+------+------------+ | val | row_number | rank | dense_rank | +------+------------+------+------------+ | 1 | 1 | 1 | 1 | | 1 | 2 | 1 | 1 | | 2 | 3 | 3 | 2 | | 3 | 4 | 4 | 3 | | 3 | 5 | 4 | 3 | | 3 | 6 | 4 | 3 | | 4 | 7 | 7 | 4 | | 4 | 8 | 7 | 4 | | 5 | 9 | 9 | 5 | +------+------------+------+------------+
ROW_NUMBER()
over_clause
Returns the number of the current row within its partition. 返回分区中当前行的编号。Rows numbers range from 1 to the number of partition rows.行数的范围从1到分区行数。
ORDER BY
affects the order in which rows are numbered. ORDER BY
影响行的编号顺序。Without 没有ORDER BY
, row numbering is nondeterministic.ORDER BY
,行号是不确定的。
ROW_NUMBER()
assigns peers different row numbers. ROW_NUMBER()
为对等方分配不同的行号。To assign peers the same value, use 要为对等方分配相同的值,请使用RANK()
or DENSE_RANK()
. RANK()
或DENSE_RANK()
。For an example, see the 有关示例,请参见RANK()
function description.RANK()
函数说明。
over_clause
is as described in Section 12.21.2, “Window Function Concepts and Syntax”.over_clause
如第12.21.2节,“窗口函数概念和语法”所述。