Window
For each row, the segment over which the pipeline is applied is determined by one of:
- , which takes a range of rows relative to the current row position.
0
references the current row.
range
, which takes a range of values relative to current row value.
For ease of use, there are two flags that override rows
or range
:
expanding:true
is an alias forrows:..0
. A sum using this window is also known as “cumulative sum”.rolling:n
is an alias forrow:(-n+1)..0
, wheren
is an integer. This will includen
last values, including current row. An average using this window is also knows as a Simple Moving Average.
from employees
group employee_id (
sort month
window rolling:12 (
derive [trail_12_m_comp = sum paycheck]
)
)
*,
SUM(paycheck) OVER (
PARTITION BY employee_id
ORDER BY
month ROWS BETWEEN 11 PRECEDING AND CURRENT ROW
) AS trail_12_m_comp
FROM
employees
SELECT
*,
AVG(value) OVER (
ORDER BY
day ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING
) AS centered_weekly_average,
SUM(value) OVER (
PARTITION BY order_month
ORDER BY
day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS monthly_running_total
FROM
orders
If you use window functions without window
transform, they will be applied to the whole table. Unlike in SQL, they will remain window functions and will not trigger aggregation.
PRQL
from employees
sort age
derive rnk = rank
SQL
PRQL
from employees
sort age
derive rnk = rank
)
SQL
SELECT
*,
RANK() OVER (
PARTITION BY department
ORDER BY
age ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS rnk
FROM
employees
There is no limitaions where windowed expressions can be used:
WITH table_1 AS (
SELECT
*,
AVG(salary) OVER () AS _expr_0
FROM
employees
)
SELECT
*
FROM
table_1 AS table_0
salary < _expr_0