12.21.1 Window Function Descriptions窗口函数描述

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窗口函数

NameDescription
CUME_DIST()Cumulative distribution value累积分布值
DENSE_RANK()Rank of current row within its partition, without gaps当前行在其分区内的秩,没有间隙
FIRST_VALUE()Value of argument from first row of window frame来自窗框第一行的参数值
LAG()Value of argument from row lagging current row within partition来自分区内当前行的参数值
LAST_VALUE()Value of argument from last row of window frame窗框最后一行的参数值
LEAD()Value of argument from row leading current row within partition分区内当前行前导行的参数值
NTH_VALUE()Value of argument from N-th row of window frame窗框第N行参数值
NTILE()Bucket number of current row within its partition.当前行在其分区内的Bucket编号。
PERCENT_RANK()Percentage rank value百分比秩值
RANK()Rank of current row within its partition, with gaps当前行在其分区内的秩,带间隙
ROW_NUMBER()Number of current row within its partition分区内当前行数

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 RESPECT NULLS (which is also the default). 它是SQL标准的一部分,但是MySQL实现只允许RESPECT NULLS(这也是默认值)。This means that NULL values are considered when calculating results. 这意味着在计算结果时会考虑NULL值。IGNORE NULLS is parsed, but produces an error.IGNORE NULLS会被解析,但会产生错误。