Update top N values using PostgreSQL
I want to update the top 10 values of a column in table. I have three columns; id , account and accountrank . To get the top 10 values I can use the following: SELECT * FROM accountrecords ORDER BY account DESC LIMIT 10; What I would like to do is to set the value in accountrank to be a series of 1 - 10 , based on the magnitude of account . Is this possible to do in PostgreSQL? WITH cte AS ( SELECT id, row_number() OVER (ORDER BY account DESC NULLS LAST) AS rn FROM accountrecords ORDER BY account DESC NULLS LAST LIMIT 10 ) UPDATE accountrecords a SET accountrank = cte.rn FROM cte WHERE cte.id