Windows can be defined and given names by which to refer to them in 窗口可以被定义和命名,在OVER
clauses. OVER
子句中引用它们。To do this, use a 为此,请使用WINDOW
clause. WINDOW
子句。If present in a query, the 如果查询中存在WINDOW
clause falls between the positions of the HAVING
and ORDER BY
clauses, and has this syntax:WINDOW
子句,则它位于HAVING
子句和ORDER BY
子句之间,并且具有以下语法:
WINDOWwindow_name
AS (window_spec
) [,window_name
AS (window_spec
)] ...
For each window definition, 对于每个窗口定义,window_name
is the window name, and window_spec
is the same type of window specification as given between the parentheses of an OVER
clause, as described in Section 12.21.2, “Window Function Concepts and Syntax”:window_name
是窗口名称,window_spec
是OVER
子句括号之间给出的同一类型的窗口规范,如第12.21.2节,“窗口函数概念和语法”所述:
window_spec
: [window_name
] [partition_clause
] [order_clause
] [frame_clause
]
A WINDOW
clause is useful for queries in which multiple OVER
clauses would otherwise define the same window. WINDOW
子句对于多个OVER
子句将定义同一个窗口的查询非常有用。Instead, you can define the window once, give it a name, and refer to the name in the 相反,您可以定义一次窗口,给它一个名称,并在OVER
clauses. OVER
子句中引用该名称。Consider this query, which defines the same window multiple times:考虑这个查询,它多次定义同一个窗口:
SELECT val, ROW_NUMBER() OVER (ORDER BY val) AS 'row_number', RANK() OVER (ORDER BY val) AS 'rank', DENSE_RANK() OVER (ORDER BY val) AS 'dense_rank' FROM numbers;
The query can be written more simply by using 通过使用WINDOW
to define the window once and referring to the window by name in the OVER
clauses:WINDOW
定义一次窗口,并在OVER
子句中按名称引用窗口,可以更简单地编写查询:
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);
A named window also makes it easier to experiment with the window definition to see the effect on query results. 命名窗口还可以更容易地试验窗口定义,以查看对查询结果的影响。You need only modify the window definition in the 只需修改WINDOW
clause, rather than multiple OVER
clause definitions.window
子句中的窗口定义,而不需要修改多个OVER
子句定义。
If an 如果OVER
clause uses OVER (
rather than window_name
...)OVER
, the named window can be modified by the addition of other clauses. window_name
OVER
子句使用OVER (
而不是window_name
...)OVER
,则可以通过添加其他子句来修改指定的窗口。window_name
For example, this query defines a window that includes partitioning, and uses 例如,此查询定义了一个包含分区的窗口,并使用ORDER BY
in the OVER
clauses to modify the window in different ways:OVER
子句中的ORDER BY
以不同的方式修改该窗口:
SELECT DISTINCT year, country, FIRST_VALUE(year) OVER (w ORDER BY year ASC) AS first, FIRST_VALUE(year) OVER (w ORDER BY year DESC) AS last FROM sales WINDOW w AS (PARTITION BY country);
An OVER
clause can only add properties to a named window, not modify them. OVER
子句只能向命名窗口添加属性,不能修改属性。If the named window definition includes a partitioning, ordering, or framing property, the 如果命名窗口定义包含分区、排序或框架属性,则引用该窗口名称的OVER
clause that refers to the window name cannot also include the same kind of property or an error occurs:OVER
子句不能同时包含相同类型的属性,否则会发生错误:
This construct is permitted because the window definition and the referring 允许此构造,因为窗口定义和涉及的OVER
clause do not contain the same kind of properties:OVER
子句不包含相同类型的属性:
OVER (w ORDER BY country) ... WINDOW w AS (PARTITION BY country)
This construct is not permitted because the 不允许此构造,因为OVER
clause specifies PARTITION BY
for a named window that already has PARTITION BY
:OVER
子句为已具有PARTITION BY
的命名窗口指定PARTITION BY
:
OVER (w PARTITION BY year) ... WINDOW w AS (PARTITION BY country)
The definition of a named window can itself begin with a 命名窗口的定义本身可以以window_name
. window_name
开始。In such cases, forward and backward references are permitted, but not cycles:在这种情况下,允许向前和向后引用,但不允许循环:
This is permitted; it contains forward and backward references but no cycles:这是允许的;它包含向前和向后引用,但没有循环:
WINDOW w1 AS (w2), w2 AS (), w3 AS (w1)
This is not permitted because it contains a cycle:这是不允许的,因为它包含一个循环:
WINDOW w1 AS (w2), w2 AS (w3), w3 AS (w1)