PgWindow

class PgWindow(name)

A WINDOW clause definition. Created via SQL.window(name). Inherits from SqlObj.

Parameters:

name – Window name string, referenced in OVER name expressions.

SQL.window('w').partition_by('department').order_by('salary DESC')
# w AS (
#     PARTITION BY department
#     ORDER BY salary DESC
# )
partition_by(expr)

Set the PARTITION BY expression.

Parameters:

expr – A string specifying the partition columns.

SQL.window('w').partition_by('department')
# w AS (
#     PARTITION BY department
# )

SQL.window('w').partition_by('dept, role')
# w AS (
#     PARTITION BY dept, role
# )
order_by(expr)

Set the ORDER BY expression.

Parameters:

expr – A string specifying the ordering.

SQL.window('w').order_by('salary DESC')
# w AS (
#     ORDER BY salary DESC
# )
frame(expr)

Set the frame clause.

Parameters:

expr – A string specifying the frame (e.g. ROWS, RANGE, GROUPS).

SQL.window('w').order_by('salary').frame('ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING')
# w AS (
#     ORDER BY salary
#     ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
# )
existing(expr)

Reference an existing window by name (window inheritance).

Parameters:

expr – Name of the existing window to inherit from.

SQL.window('ranked').existing('base_w').order_by('score DESC')
# ranked AS (
#     base_w
#     ORDER BY score DESC
# )