Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Window function (SQL)

From Wikipedia, the free encyclopedia
(Redirected fromSQL window function)
Function over multiple rows in SQL
For the term used in signal processing, seeWindow function.

InSQL, awindow function oranalytic function[1] is a function which uses values from one or multiplerows to return a value for each row. (This contrasts with anaggregate 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

[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 thePostgreSQL documentation):[3]

SELECTdepname,empno,salary,avg(salary)OVER(PARTITIONBYdepname)FROMempsalary;

Output:

 depname  | empno | salary |          avg          ----------+-------+--------+----------------------develop   |    11 |   5200 | 5020.0000000000000000develop   |     7 |   4200 | 5020.0000000000000000develop   |     9 |   4500 | 5020.0000000000000000develop   |     8 |   6000 | 5020.0000000000000000develop   |    10 |   5200 | 5020.0000000000000000personnel |     5 |   3500 | 3700.0000000000000000personnel |     2 |   3900 | 3700.0000000000000000sales     |     3 |   4800 | 4866.6666666666666667sales     |     1 |   5000 | 4866.6666666666666667sales     |     4 |   4800 | 4866.6666666666666667(10 rows)

ThePARTITION BY clause groups rows into partitions, and the function is applied to each partition separately. If thePARTITION BY clause is omitted (such as with an emptyOVER() clause), then the entireresult 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 theGROUP BY clause and non-window aggregate functions, for example).[1]

Syntax

[edit]

According to the PostgreSQL documentation, a window function has the syntax of one of the following:[4]

function_name([expression[,expression...]])OVERwindow_namefunction_name([expression[,expression...]])OVER(window_definition)function_name(*)OVERwindow_namefunction_name(*)OVER(window_definition)

wherewindow_definition has syntax:

[existing_window_name][PARTITIONBYexpression[,...]][ORDERBYexpression[ASC|DESC|USINGoperator][NULLS{FIRST|LAST}][,...]][frame_clause]

frame_clause has the syntax of one of the following:

{RANGE|ROWS|GROUPS}frame_start[frame_exclusion]{RANGE|ROWS|GROUPS}BETWEENframe_startANDframe_end[frame_exclusion]

frame_start andframe_end can beUNBOUNDED PRECEDING,offset PRECEDING,CURRENT ROW,offset FOLLOWING, orUNBOUNDED FOLLOWING.frame_exclusion can beEXCLUDE CURRENT ROW,EXCLUDE GROUP,EXCLUDE TIES, orEXCLUDE 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

[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 aframe orwindow 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 |------------      Aaron| <-- Preceding (unbounded)     Andrew|     Amelia|      James|       Jill|     Johnny| <-- 1st preceding row    Michael| <-- Current row       Nick| <-- 1st following row    Ophelia|       Zach| <-- Following (unbounded)

In the above table, the next query extracts for each row the values of a window with one preceding and one following row:

SELECTLAG(name,1)OVER(ORDERBYname)"prev",name,LEAD(name,1)OVER(ORDERBYname)"next"FROMpeopleORDERBYname

The result query contains the following values:

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

History

[edit]

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

Support for particular database implementations was added as follows:

See also

[edit]

References

[edit]
  1. ^ab"Analytic function concepts in Standard SQL | BigQuery".Google Cloud. Retrieved2021-03-23.
  2. ^"Window Functions".sqlite.org. Retrieved2021-03-23.
  3. ^"3.5. Window Functions".PostgreSQL Documentation. 2021-02-11. Retrieved2021-03-23.
  4. ^ab"4.2. Value Expressions".PostgreSQL Documentation. 2021-02-11. Retrieved2021-03-23.
  5. ^Leis, Viktor; Kundhikanjana, Kan; Kemper, Alfons; Neumann, Thomas (June 2015). "Efficient Processing of Window Functions in Analytical SQL Queries".Proc. VLDB Endow.8 (10):1058–1069.doi:10.14778/2794367.2794375.ISSN 2150-8097.
  6. ^Cao, Yu; Chan, Chee-Yong; Li, Jie; Tan, Kian-Lee (July 2012). "Optimization of Analytic Window Functions".Proc. VLDB Endow.5 (11):1244–1255.arXiv:1208.0086.doi:10.14778/2350229.2350243.ISSN 2150-8097.
  7. ^"Probably the Coolest SQL Feature: Window Functions".Java, SQL and jOOQ. 2013-11-03. Retrieved2017-09-26.
  8. ^"Window Functions in SQL - Simple Talk".Simple Talk. 2013-10-31. Retrieved2017-09-26.
  9. ^"SQL Window Functions Introduction".Apache Drill.
  10. ^"PostgreSQL: Documentation: Window Functions".www.postgresql.org. Retrieved2020-04-04.
  11. ^"Window Functions Overview".MariaDB KnowledgeBase. Retrieved2021-03-23.
  12. ^"Oracle 8i Release 2 (8.1.6) New Features".www.oracle.com. Retrieved2025-01-23.
  13. ^"Analytic Functions in Oracle 8i"(PDF).www.stanford.edu. Retrieved2025-01-23.
  14. ^"PostgreSQL Release 8.4".www.postgresql.org. 24 July 2014. Retrieved2024-03-10.
  15. ^"MySQL :: What's New in MySQL 8.0? (Generally Available)".dev.mysql.com. Retrieved2022-11-21.
  16. ^"MySQL :: MySQL 8.0 Reference Manual :: 12.21.2 Window Function Concepts and Syntax".dev.mysql.com.
  17. ^"MariaDB 10.2.0 Release Notes".mariadb.com. Retrieved2024-03-10.
  18. ^"SQLite Release 3.25.0 On 2018-09-15".www.sqlite.org. Retrieved5 February 2025.
Versions
Keywords
Related
ISO/IEC SQL parts
Retrieved from "https://en.wikipedia.org/w/index.php?title=Window_function_(SQL)&oldid=1305629449"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp