Re: Top 3 values for each group in PGSQL

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: "Ila B(dot)" <ilaria(dot)battiston(at)gmail(dot)com>
Cc: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: Top 3 values for each group in PGSQL
Date: 2019-03-01 11:59:11
Message-ID: 20190301115911.7hbvcw3pkhwz5wg5@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, Mar 01, 2019 at 11:51:24AM +0100, Ila B. wrote:
> 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.

Consider reading
https://www.depesz.com/2012/10/05/getting-top-n-rows-per-group/

Best regards,

depesz

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2019-03-01 12:52:02 Re: Top 3 values for each group in PGSQL
Previous Message Ila B. 2019-03-01 10:51:24 Top 3 values for each group in PGSQL