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.

  1. # Get the list of employees and the average salary for their dept.
  2. query = (Employee
  3. .select(
  4. Employee.name,
  5. Employee.department,
  6. Employee.salary,
  7. fn.Avg(Employee.salary).over(
  8. partition_by=[Employee.department]))
  9. .order_by(Employee.name))
  10. # Rank employees by salary.
  11. query = (Employee
  12. .select(
  13. Employee.name,
  14. Employee.salary,
  15. fn.rank().over(
  16. order_by=[Employee.salary])))

For general information on window functions, check out the postgresql docs.