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 1gzgov-0000CU-QF for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 11:59:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gzgou-0000VF-Fg for pgsql-sql@arkaria.postgresql.org; Fri, 01 Mar 2019 11:59:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzgou-0000V8-5M for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 11:59:16 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzgor-0002dl-GJ for pgsql-sql@lists.postgresql.org; Fri, 01 Mar 2019 11:59:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=OcSWFTJt5fON99+egNsbbjtQMhlHOEhpUHlfwFtX0A8=; b=aKnUTZvx/nZ0s/vjdA16jaZpgZ qlDTqUeIC1D7GJhv0VMiHKpEoeM68LV6hDwoo01nYPeXatdSEQ2MLbQlqOKQiPnn/VVsJp01IoNJI kBBKzLR6xXZ9wFdpRtjLE78uD6+9ZtcO8OWwDfKr16ovcnKat+c6L150d9I5P5j0Ne6E=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.89) (envelope-from ) id 1gzgop-0002A8-TE; Fri, 01 Mar 2019 12:59:11 +0100 Date: Fri, 1 Mar 2019 12:59:11 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: "Ila B." Cc: pgsql-sql@lists.postgresql.org Subject: Re: Top 3 values for each group in PGSQL Message-ID: <20190301115911.7hbvcw3pkhwz5wg5@depesz.com> Reply-To: depesz@depesz.com References: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <67FB61BF-CF8F-440E-B4C5-B6F72D61A9C7@gmail.com> User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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