4.2.8. Window Function Calls

4.2.8. Window Function Calls

4.2.8.窗口函数调用

A window function call represents the application of an aggregate-like function over some portion of the rows selected by a query. Unlike non-window aggregate calls, this is not tied to grouping of the selected rows into a single output row — each row remains separate in the query output. However the window function has access to all the rows that would be part of the current row's group according to the grouping specification  (PARTITION BY list) of the window function call. The syntax of a window function call is one of the following:

窗口函数调用表示在查询选择的行的某些部分上应用类似聚合的函数。与非窗口聚合调用不同,这与将所选行分组到单个输出行无关-在查询输出中,每一行保持独立。 但是,根据窗口函数调用的分组规范(PARTITION BY列表),窗口函数可以访问将成为当前行组一部分的所有行。 窗口函数的调用语法为以下之一:

function_name ([expression [, expression ... ]]) [ FILTER ( WHERE filter_clause ) ] OVER window_name

function_name ([expression [, expression ... ]]) [ FILTER ( WHERE filter_clause ) ] OVER ( window_definition )

function_name ( * ) [ FILTER ( WHERE filter_clause ) ] OVER window_name

function_name ( * ) [ FILTER ( WHERE filter_clause ) ] OVER ( window_definition )

where window_definition has the syntax

window_definition的语法:

[ existing_window_name ]

[ PARTITION BY expression [, ...] ]

[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS{ FIRST | LAST } ] [, ...] ]

[ frame_clause ]

The optional frame_clause can be one of

frame_clause可以为:

{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]

{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end

[ frame_exclusion ]

where frame_start and frame_end can be one of

frame_start和frame_end可以为其中之一:

UNBOUNDED PRECEDING

offset PRECEDING

CURRENT ROW

offset FOLLOWING

UNBOUNDED FOLLOWING

and frame_exclusion can be one of

frame_exclusion可以为其中之一:

EXCLUDE CURRENT ROW

EXCLUDE GROUP

EXCLUDE TIES

EXCLUDE NO OTHERS

Here, expression represents any value expression that does not itself contain window function calls.

此处,expression为不包含窗口函数调用的任意值表达式。

window_name is a reference to a named window specification defined in the query's WINDOW clause. Alternatively, a full window_definition can be given within parentheses, using the same syntax as for defining a named window in the WINDOW clause; see the SELECT reference page for details. It's worth pointing out that OVER wname is not exactly equivalent to OVER (wname ...); the latter implies copying and modifying the window definition, and will be rejected if the referenced window specification includes a frame clause.

window_name是对查询的WINDOW子句中定义的命名窗口规范的引用。 或者,可以使用与在WINDOW子句中定义命名窗口相同的语法在括号内给出完整的window_definition。 有关详细信息,请参见 SELECT参考页。 值得指出的是OVER wname不完全等同于OVER(wname ...); 后者意味着复制和修改窗口定义,如果引用的窗口规范包含frame子句,则将被拒绝。

The PARTITION BY clause groups the rows of the query into partitions, which are processed separately by the window function. PARTITION BY works similarly to a query-level GROUP BY clause,except that its expressions are always just expressions and cannot be output-column names or numbers.Without PARTITION BY, all rows produced by the query are treated as a single partition. The ORDER BY clause determines the order in which the rows of a partition are processed by the window function. It works similarly to a query-level ORDER BY clause, but likewise cannot use output-column names or numbers. Without ORDER BY, rows are processed in an unspecified order.

PARTITION BY子句将查询的行分组为多个分区,这些分区由window函数分别处理。PARTITION BY与查询级GROUP BY子句的工作方式相似,不同之处在于它的表达式始终只是表达式,而不能是输出列名称或数字。没有PARTITION BY的情况下,查询产生的所有行都被视为单个分区。ORDER BY子句决定窗口函数处理分区行的顺序。它的工作方式类似于查询级ORDER BY子句,但同样不能使用输出列名称或数字。 如果没有ORDER BY,则将以未指定的顺序处理行。

The frame_clause specifies the set of rows constituting the window frame, which is a subset of the current partition, for those window functions that act on the frame instead of the whole partition.The set of rows in the frame can vary depending on which row is the current row. The frame can be specified in RANGE, ROWS or GROUPS mode; in each case, it runs from the frame_start to the frame_end. If frame_end is omitted, the end defaults to CURRENT ROW.

frame_clause指定了构成窗口框架的行集合,该窗口框架是当前分区的子集,用于作用在框架上而不是整个分区的那些窗口函数。框架中的行集合因为当前行的不同而有所不同。 可以在RANGE,ROWS或GROUPS模式下指定框架;在每种情况下,它都从frame_start到frame_end运行。 如果省略frame_end,则末尾默认为CURRENT ROW。

A frame_start of UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly a frame_end of UNBOUNDED FOLLOWING means that the frame ends with the last row of the partition.

UNBOUNDED PRECEDING的frame_start表示该框架从分区的第一行开始,同样,UNBOUNDED FOLLOWING的frame_end表示该框架以分区的最后一行结束。

In RANGE or GROUPS mode, a frame_start of CURRENT ROW means the frame starts with the current row's first peer row (a row that the window's ORDER BY clause sorts as equivalent to the current row), while a frame_end of CURRENT ROW means the frame ends with the current row's last peer row. In ROWS mode, CURRENT ROW simply means the current row.

在RANGE或GROUPS模式下,CURRENT ROW的frame_start表示框架以当前行的第一对等行(窗口函数的ORDER BY子句的行排序与当前行等效)开始,而CURRENT ROW的frame_end表示框架结束与当前行的最后一个对等行。在ROWS模式下,CURRENT ROW仅表示当前行。

In the offset PRECEDING and offset FOLLOWING frame options, the offset must be an expression not containing any variables, aggregate functions, or window functions. The meaning of the offset depends on the frame mode:

• In ROWS mode, the offset must yield a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of rows before or after the current row.

• In GROUPS mode, the offset again must yield a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of peer groups before or after the current row's peer group, where a peer group is a set of rows that are equivalent in the ORDER BY ordering.(There must be an ORDER BY clause in the window definition to use GROUPS mode.)

• In RANGE mode, these options require that the ORDER BY clause specify exactly one column. The offset specifies the maximum difference between the value of that column in the current row and its value in preceding or following rows of the frame. The data type of the offset expression varies depending on the data type of the ordering column. For numeric ordering columns it is typically of the same type as the ordering column, but for datetime ordering columns it is an interval. For example, if the ordering column is of type date or timestamp, one could write RANGE BETWEEN '1 day' PRECEDING AND '10 days' FOLLOWING. The offset is still required to be non-null and non-negative, though the meaning of “non-negative” depends on its data type.

在offset PRECEDING和offset FOLLOWING框架选项中,offset必须是不包含任何变量,聚合函数或窗口函数的表达式。偏移量的含义取决于帧模式:

  • 在ROWS模式下,偏移量必须为一个非空,非负整数,并且该选项表示框架在当前行之前或之后指定的行数开始或结束。

  • 在GROUPS模式下,偏移也必须为一个非空,非负整数,并且该选项表示该框架在当前行的对等组之前或之后开始或结束指定数目的对等组,即一组与ORDER BY顺序等效的行。(在窗口定义中必须有一个ORDER BY子句才能使用GROUPS模式。)

  • 在RANGE模式下,这些选项要求ORDER BY子句仅指定一列。 偏移量指定当前行中该列的值与该框架的前一行或后几行中的列值之间的最大差值。偏移量表达式的数据类型取决于排序列的数据类型。对于数字排序列,它通常与排序列的类型相同,但是对于日期时间排序列,它是一个interval类型。 例如,如果订购列的类型为date或timestamp,则可以写“ 1天”之前和“ 10天”之间。 尽管“非负数”的含义取决于其数据类型,但仍要求偏移量必须为非空且非负数。

In any case, the distance to the end of the frame is limited by the distance to the end of the partition, so that for rows near the partition ends the frame might contain fewer rows than elsewhere.

在任何情况下,到框架末尾的距离都受到到分区末尾的距离的限制,因此对于分区末尾附近的行,该框架可能包含更少的行。

Notice that in both ROWS and GROUPS mode, 0 PRECEDING and 0 FOLLOWING are equivalent to CURRENT ROW. This normally holds in RANGE mode as well, for an appropriate data-type-specific meaning of “zero”.

请注意,在ROWS和GROUPS模式下,0 PRECEDING和0 FOLLOWING等效于CURRENT ROW。对于适当的特定于数据类型的含义“零”,这通常也适用于RANGE模式。

The frame_exclusion option allows rows around the current row to be excluded from the frame,even if they would be included according to the frame start and frame end options. EXCLUDE CURRENT ROW excludes the current row from the frame. EXCLUDE GROUP excludes the current row and its ordering peers from the frame. EXCLUDE TIES excludes any peers of the current row from the frame, but not the current row itself. EXCLUDE NO OTHERS simply specifies explicitly the default behavior of not excluding the current row or its peers.

frame_exclusion选项允许将当前行周围的行从框架中排除,即使根据开始和结束选项该行包括在内。EXCLUDE CURRENT ROW从框架中排除当前行。EXCLUDE GROUP从框架中排除当前行及其排序对等项。 EXCLUDE TIES从框架中排除当前行的任何同位体,但不排除当前行本身。 EXCLUDE NO OTHERS只是简单地明确指定不排除当前行或其对等行的默认行为。

The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this sets the frame to be all rows from the partition start up through the current row's last ORDER BY peer. Without ORDER BY, this means all rows of the partition are included in the window frame, since all rows become peers of the current row.

默认的选项是“ RANGE UNBOUNDED PRECEDING”,与“ UNBOUNDED PRECEDING and CURRENT ROWGE”之间的范围相同。 使用ORDER BY,这会将框架设置为从分区开始到当前行的最后一个ORDER BY对等点的所有行。 如果没有ORDER BY,则意味着分区的所有行都包含在窗口框架中,因为所有行都成为当前行的对等方。

Restrictions are that frame_start cannot be UNBOUNDED FOLLOWING, frame_end cannot be UNBOUNDED PRECEDING, and the frame_end choice cannot appear earlier in the above list of frame_start and frame_end options than the frame_start choice does — for example RANGE BETWEEN CURRENT ROW AND offset PRECEDING is not allowed. But, for example,ROWS BETWEEN 7 PRECEDING AND 8 PRECEDING is allowed, even though it would never select any rows.

限制条件是frame_start不能为UNBOUNDED FOLLOWING,frame_end不能为UNBOUNDED PRECEDING,并且frame_end选择不能早于上述frame_start列表,且frame_end选项不能出现在frame_start选项之前,例如,不允许RANGE BETWEEN CURRENT ROW AND offset PRECEDING。 但是,例如,允许选取7行和8行之间的行,即使它永远也不会选择任何行。

If FILTER is specified, then only the input rows for which the filter_clause evaluates to true are fed to the window function; other rows are discarded. Only window functions that are aggregates accept a FILTER clause.

如果指定了FILTER,则只有filter_clause评估为true的输入行才被窗口函数处理; 其他行将被丢弃。 只有聚合的窗口函数才接受FILTER子句。

The built-in window functions are described in Table 9.57. Other window functions can be added by the user. Also, any built-in or user-defined general-purpose or statistical aggregate can be used as a window function. (Ordered-set and hypothetical-set aggregates cannot presently be used as window functions.)

内置窗口功能在表9.57中进行了描述。用户可以添加其他窗口功能。 同样,任何内置的或用户定义的通用或统计聚合都可以用作窗口函数。 有序集和假设集的集合目前不能用作窗口函数。)

The syntaxes using * are used for calling parameter-less aggregate functions as window functions, for example count(*) OVER (PARTITION BY x ORDER BY y). The asterisk (*) is customarily not used for window-specific functions. Window-specific functions do not allow DISTINCT or ORDER BY to be used within the function argument list.

语法中使用星号(*)用于将无参数聚合函数用作窗口函数,例如count(*) OVER (PARTITION BY x ORDER BY y)。 星号(*)通常不用于特定于窗口的函数。 窗口特定的函数不允许在函数参数列表中使用DISTINCT或ORDER BY。

Window function calls are permitted only in the SELECT list and the ORDER BY clause of the query.

仅在查询的SELECT列表和ORDER BY子句中允许调用窗口函数。

More information about window functions can be found in Section 3.5, Section 9.21, and Section 7.2.5.

有关窗口功能的更多信息,请参见第3.5节9.21节7.2.5节

发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104293988