The definition of a window used with a window function can include a frame clause. 与窗口函数一起使用的窗口的定义可以包括frame子句。A frame is a subset of the current partition and the frame clause specifies how to define the subset.frame是当前分区的子集,frame子句指定如何定义子集。
Frames are determined with respect to the current row, which enables a frame to move within a partition depending on the location of the current row within its partition. 框是相对于当前行确定的,这使得框能够根据当前行在其分区内的位置在分区内移动。Examples:示例:
By defining a frame to be all rows from the partition start to the current row, you can compute running totals for each row.通过将框定义为从分区开始到当前行的所有行,可以计算每行的运行总数。
By defining a frame as extending 通过将框定义为在当前行的任一侧扩展N
rows on either side of the current row, you can compute rolling averages.N
行,可以计算滚动平均值。
The following query demonstrates the use of moving frames to compute running totals within each group of time-ordered 下面的查询演示如何使用移动框来计算每组按时间顺序排列的level
values, as well as rolling averages computed from the current row and the rows that immediately precede and follow it:level
值中的运行总计,以及从当前行以及紧跟在当前行之前和之后的行计算的滚动平均值:
mysql>SELECT
time, subject, val,
SUM(val) OVER (PARTITION BY subject ORDER BY time
ROWS UNBOUNDED PRECEDING)
AS running_total,
AVG(val) OVER (PARTITION BY subject ORDER BY time
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
AS running_average
FROM observations;
+----------+---------+------+---------------+-----------------+ | time | subject | val | running_total | running_average | +----------+---------+------+---------------+-----------------+ | 07:00:00 | st113 | 10 | 10 | 9.5000 | | 07:15:00 | st113 | 9 | 19 | 14.6667 | | 07:30:00 | st113 | 25 | 44 | 18.0000 | | 07:45:00 | st113 | 20 | 64 | 22.5000 | | 07:00:00 | xh458 | 0 | 0 | 5.0000 | | 07:15:00 | xh458 | 10 | 10 | 5.0000 | | 07:30:00 | xh458 | 5 | 15 | 15.0000 | | 07:45:00 | xh458 | 30 | 45 | 20.0000 | | 08:00:00 | xh458 | 25 | 70 | 27.5000 | +----------+---------+------+---------------+-----------------+
For the 对于running_average
column, there is no frame row preceding the first one or following the last. running_average
列,在第一个列之前或最后一个列之后没有框行。In these cases, 在这些情况下,AVG()
computes the average of the rows that are available.AVG()
计算可用行的平均值。
Aggregate functions used as window functions operate on rows in the current row frame, as do these nonaggregate window functions:用作窗口函数的聚合函数对当前行框架中的行进行操作,这些非聚合窗口函数也是如此:
FIRST_VALUE() LAST_VALUE() NTH_VALUE()
Standard SQL specifies that window functions that operate on the entire partition should have no frame clause. 标准SQL指定在整个分区上操作的窗口函数不应有frame子句。MySQL permits a frame clause for such functions but ignores it. These functions use the entire partition even if a frame is specified:MySQL允许为这些函数使用frame子句,但忽略了它。即使指定了框,这些函数也使用整个分区:
CUME_DIST() DENSE_RANK() LAG() LEAD() NTILE() PERCENT_RANK() RANK() ROW_NUMBER()
The frame clause, if given, has this syntax:frame子句(如果给定)具有以下语法:
frame_clause
:frame_units
frame_extent
frame_units
: {ROWS | RANGE}
In the absence of a frame clause, the default frame depends on whether an 在没有frame子句的情况下,默认的frame取决于是否存在ORDER BY
clause is present, as described later in this section.ORDER BY
子句,如本节后面所述。
The frame_units
value indicates the type of relationship between the current row and frame rows:frame_units
值表示当前行和框行之间的关系类型:
ROWS
: The frame is defined by beginning and ending row positions. :框由起始行和结束行位置定义。Offsets are differences in row numbers from the current row number.偏移是行号与当前行号之间的差异。
RANGE
: The frame is defined by rows within a value range. :框由值范围内的行定义。Offsets are differences in row values from the current row value.偏移是行值与当前行值之间的差异。
The frame_extent
value indicates the start and end points of the frame. frame_extent
值指示框的起点和终点。You can specify just the start of the frame (in which case the current row is implicitly the end) or use 您可以仅指定框的开始(在这种情况下,当前行隐式地为结束)或使用BETWEEN
to specify both frame endpoints:BETWEEN
指定两个框端点:
frame_extent
: {frame_start
|frame_between
}frame_between
: BETWEENframe_start
ANDframe_end
frame_start
,frame_end
: { CURRENT ROW | UNBOUNDED PRECEDING | UNBOUNDED FOLLOWING |expr
PRECEDING |expr
FOLLOWING }
With 对于BETWEEN
syntax, frame_start
must not occur later than frame_end
.BETWEEN
语法,frame_start
不能晚于frame_end
。
The permitted 允许的frame_start
and frame_end
values have these meanings:frame_start
和frame_end
值具有以下含义:
CURRENT ROW
: For :对于ROWS
, the bound is the current row. ROWS
,绑定为当前行。For 对于RANGE
, the bound is the peers of the current row.RANGE
,边界是当前行的对等方。
UNBOUNDED PRECEDING
: The bound is the first partition row.:绑定是第一个分区行。
UNBOUNDED FOLLOWING
: The bound is the last partition row.:绑定是最后一个分区行。
expr
PRECEDING: For :对于ROWS
, the bound is expr
rows before the current row. ROWS
,绑定是在当前行之前的expr
行。For 对于RANGE
, the bound is the rows with values equal to the current row value minus expr
; if the current row value is NULL
, the bound is the peers of the row.RANGE
,界限是值等于当前行值减去expr
的行;如果当前行值为NULL
,则绑定为该行的对等方。
For 对于
(and expr
PRECEDING
), expr
FOLLOWINGexpr
can be a ?
parameter marker (for use in a prepared statement), a nonnegative numeric literal, or a temporal interval of the form INTERVAL
. val
unit
(以及expr
PRECEDING
),expr
FOLLOWINGexpr
可以是?
参数标记(在准备好的语句中使用)、非负数字文字或形式为INTERVAL
的时间间隔。val
unit
For 对于INTERVAL
expressions, val
specifies nonnegative interval value, and unit
is a keyword indicating the units in which the value should be interpreted. INTERVAL
表达式,val
指定非负的间隔值,unit
是一个关键字,指示应在其中解释值的单位。(For details about the permitted (有关允许的units
specifiers, see the description of the DATE_ADD()
function in Section 12.7, “Date and Time Functions”.)unit
说明符的详细信息,请参阅第12.7节,“日期和时间函数”中的DATE_ADD()
函数的说明。)
数值表达式或时态表达式上的RANGE
on a numeric or temporal expr
requires ORDER BY
on a numeric or temporal expression, respectively.RANGE
分别要求数值表达式或时态表达式上的ORDER BY
。
Examples of valid 有效的
and expr
PRECEDING
indicators:expr
FOLLOWING
指示符和expr
PRECEDING
指示符示例:expr
FOLLOWING
10 PRECEDING INTERVAL 5 DAY PRECEDING 5 FOLLOWING INTERVAL '2:30' MINUTE_SECOND FOLLOWING
expr
FOLLOWING: For :对于ROWS
, the bound is expr
rows after the current row. ROW
,绑定是在当前行之后expr
行。For 对于RANGE
, the bound is the rows with values equal to the current row value plus expr
; if the current row value is NULL
, the bound is the peers of the row.RANGE
,界限是值等于当前行值加上expr
的行;如果当前行值为NULL
,则绑定为该行的对等方。
For permitted values of 有关expr
, see the description of
.expr
PRECEDINGexpr
的允许值,请参见前面expr
的说明。
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
。
In the absence of a frame clause, the default frame depends on whether an 如果没有框架子句,则默认框架取决于是否存在ORDER BY子句:ORDER BY
clause is present:
With 使用ORDER BY
: The default frame includes rows from the partition start through the current row, including all peers of the current row (rows equal to the current row according to the ORDER BY
clause). ORDER BY
:默认框架包括从分区开头到当前行的行,包括当前行的所有对等方(根据ORDER BY
子句,行等于当前行)。The default is equivalent to this frame specification:默认值相当于此框架规范:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
Without 不用ORDER BY
: The default frame includes all partition rows (because, without ORDER BY
, all partition rows are peers). ORDER BY
:默认框架包括所有分区行(因为,不带ORDER BY
,所有分区行都是对等的)。The default is equivalent to this frame specification:默认值相当于此框架规范:
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
Because the default frame differs depending on presence or absence of 由于默认框因是否存在ORDER BY
, adding ORDER BY
to a query to get deterministic results may change the results. ORDER BY
而不同,因此向查询中添加ORDER BY
以获得确定性结果可能会更改结果。(For example, the values produced by (例如,SUM()
might change.) SUM()
生成的值可能会更改。)To obtain the same results but ordered per 若要获得相同的结果但按ORDER BY
, provide an explicit frame specification to be used regardless of whether ORDER BY
is present.ORDER BY
排序,请提供一个无论ORDER BY
是否存在都要使用的显式框规范。
The meaning of a frame specification can be nonobvious when the current row value is 当当前行值为NULL
. NULL
时,框规范的含义可能不明显。Assuming that to be the case, these examples illustrate how various frame specifications apply:假设是这样,这些示例说明了各种框架规范是如何应用的:
ORDER BY X ASC RANGE BETWEEN 10 FOLLOWING AND 15 FOLLOWING
The frame starts at 框从NULL
and stops at NULL
, thus includes only rows with value NULL
.NULL
开始,在NULL
结束,因此只包括值为NULL
的行。
ORDER BY X ASC RANGE BETWEEN 10 FOLLOWING AND UNBOUNDED FOLLOWING
The frame starts at 框从NULL
and stops at the end of the partition. NULL
开始,在分区的末尾停止。Because an 因为ASC
sort puts NULL
values first, the frame is the entire partition.ASC
排序将空值放在第一位,所以框是整个分区。
ORDER BY X DESC RANGE BETWEEN 10 FOLLOWING AND UNBOUNDED FOLLOWING
The frame starts at 框从NULL
and stops at the end of the partition. NULL
开始,在分区的末尾停止。Because a 因为DESC
sort puts NULL
values last, the frame is only the NULL
values.DESC
排序将NULL
值放在最后,所以框仅是NULL
值。
ORDER BY X ASC RANGE BETWEEN 10 PRECEDING AND UNBOUNDED FOLLOWING
The frame starts at 框从NULL
and stops at the end of the partition. NULL
开始,在分区的末尾停止。Because an 因为ASC
sort puts NULL
values first, the frame is the entire partition.ASC
排序将空值放在第一位,所以框是整个分区。
ORDER BY X ASC RANGE BETWEEN 10 PRECEDING AND 10 FOLLOWING
The frame starts at 框从NULL
and stops at NULL
, thus includes only rows with value NULL
.NULL
开始,在NULL
结束,因此只包括值为NULL
的行。
ORDER BY X ASC RANGE BETWEEN 10 PRECEDING AND 1 PRECEDING
The frame starts at 框从NULL
and stops at NULL
, thus includes only rows with value NULL
.NULL
开始,在NULL
结束,因此只包括值为NULL
的行。
ORDER BY X ASC RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING
The frame starts at the beginning of the partition and stops at rows with value 该框从分区的开头开始,在值为NULL
. NULL
的行处停止。Because an 因为ASC排序将空值放在第一位,所以框只是空值。ASC
sort puts NULL
values first, the frame is only the NULL
values.