Ecto.Query.windows
You're seeing just the macro
windows
, go back to Ecto.Query module for more information.
Defines windows which can be used with Ecto.Query.WindowAPI
.
Receives a keyword list where keys are names of the windows and values are a keyword list with window expressions.
Examples
# Compare each employee's salary with the average salary in his or her department
from e in Employee,
select: {e.depname, e.empno, e.salary, over(avg(e.salary), :department)},
windows: [department: [partition_by: e.depname]]
In the example above, we get the average salary per department.
:department
is the window name, partitioned by e.depname
and avg/1
is the window function. For more information
on windows functions, see Ecto.Query.WindowAPI
.
Window expressions
The following keys are allowed when specifying a window.
:partition_by
A list of fields to partition the window by, for example:
windows: [department: [partition_by: e.depname]]
A list of atoms can also be interpolated for dynamic partitioning:
fields = [:depname, :year]
windows: [dynamic_window: [partition_by: ^fields]]
:order_by
A list of fields to order the window by, for example:
windows: [ordered_names: [order_by: e.name]]
It works exactly as the keyword query version of order_by/3
.
:frame
A fragment which defines the frame for window functions.
Examples
# Compare each employee's salary for each month with his average salary for previous 3 months
from p in Payroll,
select: {p.empno, p.date, p.salary, over(avg(p.salary), :prev_months)},
windows: [prev_months: [partition_by: p.empno, order_by: p.date, frame: fragment("ROWS 3 PRECEDING EXCLUDE CURRENT ROW")]]