Window functions
peewee comes with support for SQL window functions, which can be created by calling Function.over()
and passing in your partitioning or ordering parameters.
# Get the list of employees and the average salary for their dept.
query = (Employee
.select(
Employee.name,
Employee.department,
Employee.salary,
fn.Avg(Employee.salary).over(
partition_by=[Employee.department]))
.order_by(Employee.name))
# Rank employees by salary.
query = (Employee
.select(
Employee.name,
Employee.salary,
fn.rank().over(
order_by=[Employee.salary])))
For general information on window functions, check out the postgresql docs.