Chapter 10. Window (Analytical) Functions

According to the SQL specification, window functions (also known as analytical functions) are a kind of aggregation, but one that does not filter the result set of a query. The rows of aggregated data are mixed with the query result set.

The window functions are used with the OVER clause. They may appear only in the SELECT list, or the ORDER BY clause of a query.

Firebird window functions may be partitioned and ordered.

Syntax

  1. <window_function> ::=
  2. <aggregate_function> OVER <window_name_or_spec>
  3. | <window_function_name> ([<expr> [, <expr> ...]])
  4. OVER <window_name_or_spec>
  5. <aggregate_function> ::=
  6. !! See Aggregate Functions !!
  7. <window_name_or_spec> ::=
  8. <window_specification> | existing_window_name
  9. <window_function_name> ::=
  10. <ranking_function>
  11. | <navigational_function>
  12. <ranking_function> ::=
  13. RANK | DENSE_RANK | PERCENT_RANK | ROW_NUMBER
  14. | CUME_DIST | NTILE
  15. <navigational_function>
  16. LEAD | LAG | FIRST_VALUE | LAST_VALUE | NTH_VALUE
  17. <window_specification> ::=
  18. ( [existing_window_name] [<window_partition>]
  19. [<window_order>] [<window_frame>] )
  20. <window_partition> ::=
  21. [PARTITION BY <expr> [, <expr> ...]]
  22. <window_order> ::=
  23. [ORDER BY
  24. <expr> [<direction>] [<nulls_placement>]
  25. [, <expr> [<direction>] [<nulls_placement>] ...]
  26. <direction> ::= {ASC | DESC}
  27. <nulls_placement> ::= NULLS {FIRST | LAST}
  28. <window_frame> ::= {RANGE | ROWS} <window_frame_extent>
  29. <window_frame_extent> ::=
  30. <window_frame_start> | <window_frame_between>
  31. <window_frame_start> ::=
  32. UNBOUNDED PRECEDING | <expr> PRECEDING | CURRENT ROW
  33. <window_frame_between> ::=
  34. BETWEEN <window_frame_bound_1> AND <window_frame_bound_2>
  35. <window_frame_bound_1> ::=
  36. UNBOUNDED PRECEDING | <expr> PRECEDING | CURRENT ROW
  37. | <expr> FOLLOWING
  38. <window_frame_bound_2> ::=
  39. <expr> PRECEDING | CURRENT ROW | <expr> FOLLOWING
  40. | UNBOUNDED FOLLOWING

Table 10.1 Window Function Arguments

ArgumentDescription

expr

Expression. May contain a table column, constant, variable, expression, scalar or aggregate function. Window functions are not allowed as an expression.

aggregate_function

An aggregate function used as a window function

existing_window_name

A named window defined using the WINDOW clause of the current query specification.

10.1 Aggregate Functions as Window Functions

All aggregate functions — including FILTER clause — can be used as window functions, by adding the OVER clause.

Imagine a table EMPLOYEE with columns ID, NAME and SALARY, and the need to show each employee with their respective salary and the percentage of their salary over the payroll.

A normal query could achieve this, as follows:

  1. select
  2. id,
  3. department,
  4. salary,
  5. salary / (select sum(salary) from employee) portion
  6. from employee
  7. order by id;

Results

  1. id department salary portion
  2. -- ---------- ------ ----------
  3. 1 R & D 10.00 0.2040
  4. 2 SALES 12.00 0.2448
  5. 3 SALES 8.00 0.1632
  6. 4 R & D 9.00 0.1836
  7. 5 R & D 10.00 0.2040

The query is repetitive and lengthy to run, especially if EMPLOYEE happens to be a complex view.

The same query could be specified in a much faster and more elegant way using a window function:

  1. select
  2. id,
  3. department,
  4. salary,
  5. salary / sum(salary) OVER () portion
  6. from employee
  7. order by id;

Here, sum(salary) over () is computed with the sum of all SALARY from the query (the EMPLOYEE table).