The WINDOW
Clause
The WINDOW
clause defines one or more named windows that can be referenced by window functions in the current query specification.
<query_spec> ::= SELECT [<limit_clause>] [<distinct_clause>] <select_list> <from_clause> [<where_clause>] [<group_clause>] [<having_clause>] [<named_windows_clause>] [<plan_clause>] <named_windows_clause> ::= WINDOW <window_definition> [, <window_definition> ...] <window definition> ::= new-window-name AS (<window-specification-details>) <window-specification-details> ::= !! See Window (Analytical) Functions !!
In a query with multiple SELECT
and WINDOW
clauses (for example, with subqueries), the scope of the `new_window_name_ is confined to its query context.That means a window name from an inner context cannot be used in an outer context, nor vice versa.However, the same window name can be used independently in different contexts, though to avoid confusion it might be better to avoid this.
For more information, see [fblangref50-windowfuncs].
Example Using Named Windows
select
id,
department,
salary,
count(*) over w1,
first_value(salary) over w2,
last_value(salary) over w2
from employee
window w1 as (partition by department),
w2 as (w1 order by salary)
order by department, salary;