Top 3 values for each group in PGSQL

From: "Ila B(dot)" <ilaria(dot)battiston(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Top 3 values for each group in PGSQL
Date: 2019-03-01 10:51:24
Message-ID: 67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message hubert depesz lubaczewski 2019-03-01 11:59:11 Re: Top 3 values for each group in PGSQL
Previous Message github kran 2019-02-27 05:51:58 Re: PostgreSQL DB Maintenance and Partitioning to keep data longer.