Window Function (SQL)

tosh2 pts0 comments

Window function (SQL) - Wikipedia

Jump to content

Search

Search

Donate

Create account

Log in

Personal tools

Donate

Create account

Log in

Window function (SQL)

6 languages

Deutsch<br>فارسی<br>Français<br>日本語<br>Українська<br>中文

Edit links

From Wikipedia, the free encyclopedia

Function over multiple rows in SQL

For the term used in signal processing, see Window function.

In SQL, a window function or analytic function [1] is a function which uses values from one or multiple rows to return a value for each row. (This contrasts with an aggregate function, which returns a single value for multiple rows.) Window functions have an OVER clause; any function without an OVER clause is not a window function, but rather an aggregate or single-row (scalar) function.[2]

Example<br>[edit]

As an example, here is a query which uses a window function to compare the salary of each employee with the average salary of their department (example from the PostgreSQL documentation):[3]<br>SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;

Output:

depname | empno | salary | avg<br>----------+-------+--------+----------------------<br>develop | 11 | 5200 | 5020.0000000000000000<br>develop | 7 | 4200 | 5020.0000000000000000<br>develop | 9 | 4500 | 5020.0000000000000000<br>develop | 8 | 6000 | 5020.0000000000000000<br>develop | 10 | 5200 | 5020.0000000000000000<br>personnel | 5 | 3500 | 3700.0000000000000000<br>personnel | 2 | 3900 | 3700.0000000000000000<br>sales | 3 | 4800 | 4866.6666666666666667<br>sales | 1 | 5000 | 4866.6666666666666667<br>sales | 4 | 4800 | 4866.6666666666666667<br>(10 rows)<br>The PARTITION BY clause groups rows into partitions, and the function is applied to each partition separately. If the PARTITION BY clause is omitted (such as with an empty OVER() clause), then the entire result set is treated as a single partition.[4] For this query, the average salary reported would be the average taken over all rows.

Window functions are evaluated after aggregation (after the GROUP BY clause and non-window aggregate functions, for example).[1]

Syntax<br>[edit]

According to the PostgreSQL documentation, a window function has the syntax of one of the following:[4]<br>function_name ([expression [, expression ... ]]) OVER window_name<br>function_name ([expression [, expression ... ]]) OVER ( window_definition )<br>function_name ( * ) OVER window_name<br>function_name ( * ) OVER ( window_definition )

where window_definition has syntax:<br>[ existing_window_name ]<br>[ PARTITION BY expression [, ...] ]<br>[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]<br>[ frame_clause ]

frame_clause has the syntax of one of the following:<br>{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]<br>{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]

frame_start and frame_end can be UNBOUNDED PRECEDING, offset PRECEDING, CURRENT ROW, offset FOLLOWING, or UNBOUNDED FOLLOWING. frame_exclusion can be EXCLUDE CURRENT ROW, EXCLUDE GROUP, EXCLUDE TIES, or EXCLUDE NO OTHERS.

expression refers to any expression that does not contain a call to a window function.

Notation:

Brackets [] indicate optional clauses

Curly braces {} indicate a set of different possible options, with each option delimited by a vertical bar |

Example<br>[edit]

Window functions allow access to data in the records right before and after the current record.[5][6][7][8] A window function defines a frame or window of rows with a given length around the current row, and performs a calculation across the set of data in the window.[9][10]

NAME |<br>Aaron|<br>In the above table, the next query extracts for each row the values of a window with one preceding and one following row:

SELECT<br>LAG(name, 1)<br>OVER(ORDER BY name) "prev",<br>name,<br>LEAD(name, 1)<br>OVER(ORDER BY name) "next"<br>FROM people<br>ORDER BY name

The result query contains the following values:

| PREV | NAME | NEXT |<br>|----------|----------|----------|<br>| (null)| Aaron| Amelia|<br>| Aaron| Amelia| Andrew|<br>| Amelia| Andrew| James|<br>| Andrew| James| Jill|<br>| James| Jill| Johnny|<br>| Jill| Johnny| Michael|<br>| Johnny| Michael| Nick|<br>| Michael| Nick| Ophelia|<br>| Nick| Ophelia| Zach|<br>| Ophelia| Zach| (null)|

History<br>[edit]

Window functions were incorporated into the SQL:2003 standard and had functionality expanded in later specifications.[11]

Support for particular database implementations was added as follows:

Oracle - version 8.1.6 in 2000.[12][13]

PostgreSQL - version 8.4 in 2009.[14]

MySQL - version 8 in 2018.[15][16]

MariaDB - version 10.2 in 2016.[17]

SQLite - release 3.25.0 in 2018.[18]

See also<br>[edit]

Select (SQL) § Limiting result rows

References<br>[edit]

1 2 "Analytic function concepts in Standard SQL | BigQuery". Google Cloud. Retrieved 2021-03-23.

↑ "Window Functions". sqlite.org. Retrieved 2021-03-23.

↑ "3.5. Window Functions". PostgreSQL Documentation. 2021-02-11. Retrieved 2021-03-23.

1 2 "4.2. Value Expressions". PostgreSQL Documentation. 2021-02-11. Retrieved 2021-03-23.

↑...

window function rows expression name edit

Related Articles