agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedTop 3 values for each group in PGSQL
4+ messages / 4 participants
[nested] [flat]
* Top 3 values for each group in PGSQL
@ 2019-03-01 10:51 Ila B. <ilaria.battiston@gmail.com>
0 siblings, 2 replies; 4+ messages in thread
From: Ila B. @ 2019-03-01 10:51 UTC (permalink / raw)
To: pgsql-sql@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Top 3 values for each group in PGSQL
@ 2019-03-01 11:59 hubert depesz lubaczewski <depesz@depesz.com>
parent: Ila B. <ilaria.battiston@gmail.com>
1 sibling, 0 replies; 4+ messages in thread
From: hubert depesz lubaczewski @ 2019-03-01 11:59 UTC (permalink / raw)
To: Ila B. <ilaria.battiston@gmail.com>; +Cc: pgsql-sql@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Top 3 values for each group in PGSQL
@ 2019-03-01 12:52 Thomas Kellerer <spam_eater@gmx.net>
parent: Ila B. <ilaria.battiston@gmail.com>
1 sibling, 1 reply; 4+ messages in thread
From: Thomas Kellerer @ 2019-03-01 12:52 UTC (permalink / raw)
To: pgsql-sql@lists.postgresql.org
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;
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Top 3 values for each group in PGSQL
@ 2019-03-01 13:17 Achilleas Mantzios <achill@matrix.gatewaynet.com>
parent: Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 0 replies; 4+ messages in thread
From: Achilleas Mantzios @ 2019-03-01 13:17 UTC (permalink / raw)
To: pgsql-sql@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2019-03-01 13:17 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-03-01 10:51 Top 3 values for each group in PGSQL Ila B. <ilaria.battiston@gmail.com>
2019-03-01 11:59 ` hubert depesz lubaczewski <depesz@depesz.com>
2019-03-01 12:52 ` Thomas Kellerer <spam_eater@gmx.net>
2019-03-01 13:17 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox