pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: Ila B. <ilaria.battiston@gmail.com>
Cc: pgsql-sql@lists.postgresql.org
Subject: Re: Top 3 values for each group in PGSQL
Date: Fri, 1 Mar 2019 12:59:11 +0100
Message-ID: <20190301115911.7hbvcw3pkhwz5wg5@depesz.com> (raw)
In-Reply-To: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com>
References: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com>

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





view thread (4+ messages)  latest in thread

Message-ID: <20190301115911.7hbvcw3pkhwz5wg5@depesz.com>
Permalink:  ../20190301115911.7hbvcw3pkhwz5wg5@depesz.com/
Also on:    postgresql.org/message-id/20190301115911.7hbvcw3pkhwz5wg5@depesz.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: depesz@depesz.com, ilaria.battiston@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: Re: Top 3 values for each group in PGSQL
  In-Reply-To: <20190301115911.7hbvcw3pkhwz5wg5@depesz.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox