agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: pgsql-sql@lists.postgresql.org
Subject: Re: Top 3 values for each group in PGSQL
Date: Fri, 1 Mar 2019 15:17:48 +0200
Message-ID: <bef5c3a6-7f3c-d8f2-259e-54735dee90f7@matrix.gatewaynet.com> (raw)
In-Reply-To: <50d9b39f-56f8-8bc8-a93a-d5feb16f4dd3@gmx.net>
References: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com>
<50d9b39f-56f8-8bc8-a93a-d5feb16f4dd3@gmx.net>
On 1/3/19 2:52 μ.μ., Thomas Kellerer wrote:
> Ila B. schrieb am 01.03.2019 um 11:51:
>> 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.
> Something along the lines:
>
> select code, year, "count"
> from (
> select code, year, "count",
> dense_rank() over (partition by code, year order by "count" desc) as rnk
> from the_table
> ) t
> where rnk <= 3;
Yup, that's the idea
select qryout.* FROM (select qry.*,dense_rank() OVER (ORDER BY count DESC) FROM (select id_1,year_built,count(*) from vessels WHERE year_built IS NOT NULL AND year_built<>'' group by id_1,year_built
ORDER BY COUNT(*) DESC) as qry ) qryout WHERE dense_rank<=3;
id_1 | year_built | count | dense_rank
------+------------+-------+------------
94 | 2009 | 11 | 1
97 | 2010 | 10 | 2
94 | 2011 | 10 | 2
94 | 1975 | 9 | 3
94 | 1976 | 9 | 3
(5 rows)
or
select qryout.* FROM (select qry.*,dense_rank() OVER (ORDER BY count DESC) FROM (select distinct id_1,year_built,count(*) OVER (partition by id_1,year_built) from vessels WHERE year_built IS NOT NULL
AND year_built<>'') as qry ) qryout WHERE dense_rank<=3;
id_1 | year_built | count | dense_rank
------+------------+-------+------------
94 | 2009 | 11 | 1
97 | 2010 | 10 | 2
94 | 2011 | 10 | 2
94 | 1975 | 9 | 3
94 | 1976 | 9 | 3
(5 rows)
>
--
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt
view thread (4+ messages)
Message-ID: <bef5c3a6-7f3c-d8f2-259e-54735dee90f7@matrix.gatewaynet.com>
Permalink: ../bef5c3a6-7f3c-d8f2-259e-54735dee90f7@matrix.gatewaynet.com/
Also on: postgresql.org/message-id/bef5c3a6-7f3c-d8f2-259e-54735dee90f7@matrix.gatewaynet.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: achill@matrix.gatewaynet.com, pgsql-sql@lists.postgresql.org
Subject: Re: Top 3 values for each group in PGSQL
In-Reply-To: <bef5c3a6-7f3c-d8f2-259e-54735dee90f7@matrix.gatewaynet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox