Top 3 values for each group in PGSQL

"Ila B." <[email protected]> Fri, 1 Mar 2019 11:51:24 +0100
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <[email protected]>
Hello,

I’m working on a health database and I’m trying to extract the most popular prescription codes from a custom table I structured like this:

Year - Code - Count(code)

I want to extract the 3 codes with maximum count for each year. I know I should be using rank() but I don’t really understand how this works.
I am using pgAdmin4 version 3.5 with PostgreSQL 10.6 on Windows 10 Pro and no permission to update.

Thank you in advance,
Ilaria