DDSQL Window Functions

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Join the Beta!

DDSQL is in private beta.

Request Access

Overview

A window function applies an aggregation to some subset of the rows selected by a query. The selected rows are preserved in the query output, rather than being grouped into a single output row as they would be in a non-window aggregation.

For details on how window functions work, see the Postgres documentation for Window Function.

Syntax

function_name ([expression [, expression ...]]) OVER (
  [ PARTITION BY expression [, ...] ]
  [ ORDER BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
  [ frame_clause ]
)

The optional frame_clause has the following syntax:

{ RANGE | ROWS } frame_start
| { RANGE | ROWS } BETWEEN frame_start AND frame_end

The frame_start and frame_end expressions can be one of the following:

  • UNBOUNDED PRECEDING
  • offset PRECEDING
  • CURRENT ROW
  • offset FOLLOWING
  • UNBOUNDED FOLLOWING

Functions

The functions below can be used in windows, along with the aggregation functions.

row_number

NameReturn typeDescription
row_number()integerReturns the number of the current row within its partition, counting from 1.

rank

NameReturn typeDescription
rank()integerReturns the rank of the current row, with gaps (the row_number of the first row in its peer group).

dense_rank

NameReturn typeDescription
dense_rank()integerReturns the rank of the current row, without gaps. This function effectively counts peer groups.

first_value

NameReturn typeDescription
first_value(value T)TReturns the value evaluated at the row that is the first row of the window frame.

last_value

NameReturn typeDescription
last_value(value T)TReturns the value evaluated at the row that is the last row of the window frame.