
# select person, dept, salary, rank ( ) over ( partition by dept order by salary ), row_number ( ) over (partition by dept order by salary ) from test I wrote about them long time ago, but with 8.4 you can write it much simpler: The 2 most common examples of such calculations are “rownum" and cumulative sum.

#Postgresql window functions windows#
Windows let you say that you want to calculate something for only part of the partition. It means that given “average" salary was average to whole department. Now to the windows.Īs you saw, all window functions that I called till now, worked on all rows in given partition. So, I hope you understand what partition is. To quickly check who (in given department) is the most overpaid. Salary * 100 / avg (salary ) over ( partition by dept ) as numeric ( 5, 2 ) ) as percent_of_average

# select person, dept, salary, avg (salary ) over ( partition by dept ) from test order by person īradley | marketing | 1000 | 1325.0000000000000000 # select person, dept, salary, avg (salary ) over ( partition by dept ) from test
