Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.21. Window Functions
Prev UpChapter 9. Functions and OperatorsHome Next

9.21. Window Functions

Window functions provide the ability to perform calculations across sets of rows that are related to the current query row. SeeSection 3.5 for an introduction to this feature, andSection 4.2.8 for syntax details.

The built-in window functions are listed inTable 9.56. Note that these functionsmust be invoked using window function syntax; that is anOVER clause is required.

In addition to these functions, any built-in or user-defined normal aggregate function (but not ordered-set or hypothetical-set aggregates) can be used as a window function; seeSection 9.20 for a list of the built-in aggregates. Aggregate functions act as window functions only when anOVER clause follows the call; otherwise they act as regular aggregates.

Table 9.56. General-Purpose Window Functions

FunctionReturn TypeDescription
row_number()bigintnumber of the current row within its partition, counting from 1
rank()bigintrank of the current row with gaps; same asrow_number of its first peer
dense_rank()bigintrank of the current row without gaps; this function counts peer groups
percent_rank()double precisionrelative rank of the current row: (rank - 1) / (total rows - 1)
cume_dist()double precisionrelative rank of the current row: (number of rows preceding or peer with current row) / (total rows)
ntile(num_bucketsinteger)integerinteger ranging from 1 to the argument value, dividing the partition as equally as possible
lag(valueanyelement [,offsetinteger [,defaultanyelement ]])same type asvalue returnsvalue evaluated at the row that isoffset rows before the current row within the partition; if there is no such row, instead returndefault (which must be of the same type asvalue). Bothoffset anddefault are evaluated with respect to the current row. If omitted,offset defaults to 1 anddefault to null
lead(valueanyelement [,offsetinteger [,defaultanyelement ]])same type asvalue returnsvalue evaluated at the row that isoffset rows after the current row within the partition; if there is no such row, instead returndefault (which must be of the same type asvalue). Bothoffset anddefault are evaluated with respect to the current row. If omitted,offset defaults to 1 anddefault to null
first_value(valueany)same type asvalue returnsvalue evaluated at the row that is the first row of the window frame
last_value(valueany)same type asvalue returnsvalue evaluated at the row that is the last row of the window frame
nth_value(valueany,nthinteger)same type asvalue returnsvalue evaluated at the row that is thenth row of the window frame (counting from 1); null if no such row

All of the functions listed inTable 9.56 depend on the sort ordering specified by theORDER BY clause of the associated window definition. Rows that are not distinct in theORDER BY ordering are said to bepeers; the four ranking functions are defined so that they give the same answer for any two peer rows.

Note thatfirst_value,last_value, andnth_value consider only the rows within thewindow frame, which by default contains the rows from the start of the partition through the last peer of the current row. This is likely to give unhelpful results forlast_value and sometimes alsonth_value. You can redefine the frame by adding a suitable frame specification (RANGE orROWS) to theOVER clause. SeeSection 4.2.8 for more information about frame specifications.

When an aggregate function is used as a window function, it aggregates over the rows within the current row's window frame. An aggregate used withORDER BY and the default window frame definition produces arunning sum type of behavior, which may or may not be what's wanted. To obtain aggregation over the whole partition, omitORDER BY or useROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Other frame specifications can be used to obtain other effects.

Note

The SQL standard defines aRESPECT NULLS orIGNORE NULLS option forlead,lag,first_value,last_value, andnth_value. This is not implemented inPostgreSQL: the behavior is always the same as the standard's default, namelyRESPECT NULLS. Likewise, the standard'sFROM FIRST orFROM LAST option fornth_value is not implemented: only the defaultFROM FIRST behavior is supported. (You can achieve the result ofFROM LAST by reversing theORDER BY ordering.)


Prev Up Next
9.20. Aggregate Functions Home 9.22. Subquery Expressions
epubpdf
Go to PostgreSQL 9.6
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp