Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzi3F-0004ID-Ic for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 13:18:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gzi3E-0004qh-7w for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 13:18:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzi3D-0004oK-QD for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 13:18:07 +0000 Received: from hermes.gatewaynet.com ([193.92.121.98]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzi34-0001ZL-Fj for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 13:18:06 +0000 Received: from smadev.internal.net (pc131.internal.net [10.9.200.131]) by hermes.gatewaynet.com (8.15.2/8.15.2/Debian-8) with ESMTPS id x21DHnMB010724 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 1 Mar 2019 15:17:54 +0200 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id x21DHmu3000519 for ; Fri, 1 Mar 2019 15:17:48 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: Top 3 values for each group in PGSQL To: pgsql-sql@lists.postgresql.org References: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com> <50d9b39f-56f8-8bc8-a93a-d5feb16f4dd3@gmx.net> From: Achilleas Mantzios Message-ID: Date: Fri, 1 Mar 2019 15:17:48 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Thunderbird/60.3.2 MIME-Version: 1.0 In-Reply-To: <50d9b39f-56f8-8bc8-a93a-d5feb16f4dd3@gmx.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.100.2 at hermes X-Virus-Status: Clean X-Spam-Status: No, score=-32.9 required=5.0 tests=ALL_TRUSTED,DYN_HAM_COORD, DYN_HAM_IT,DYN_HAM_NAUTICAL,DYN_HAM_VMA_BODY,TW_QR autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on hermes.gatewaynet.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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