Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 98D2D9FA5D8 for ; Thu, 19 Oct 2006 13:55:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 18914-10 for ; Thu, 19 Oct 2006 16:55:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 01C109FA12F for ; Thu, 19 Oct 2006 13:55:32 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id EF1E856446; Thu, 19 Oct 2006 11:55:31 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Thu, 19 Oct 2006 11:55:29 -0500 Date: Thu, 19 Oct 2006 11:55:29 -0500 From: "Jim C. Nasby" To: Mezei Zolt??n Cc: pgsql-sql@postgresql.org Subject: Re: Grouping by day, limiting amounts Message-ID: <20061019165528.GI71084@nasby.net> References: <453766DB.6000408@telefor.hu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <453766DB.6000408@telefor.hu> X-Operating-System: FreeBSD 6.1-RELEASE-p10 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.13 (2006-08-11) X-Hashcash: 1:20:061019:mezei.zoltan@telefor.hu::uGkrpvmJCIZrW3sI:00000000000000 000000000000000000000000F1Ft X-Hashcash: 1:20:061019:pgsql-sql@postgresql.org::Z79Wum0gHfHF3g5E:0000000000000 0000000000000000000000006H74 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/180 X-Sequence-Number: 26569 On Thu, Oct 19, 2006 at 01:51:55PM +0200, Mezei Zolt??n wrote: > > Hi, > I didn't really know what subject I should give. > I have a table like this one: > 2006.10.01. Bela 10 > 2006.10.01. Aladar 9 > 2006.10.01. Cecil 8 > 2006.10.01. Dezso 7 > 2006.10.01. Elemer 6 > 2006.10.02. Bela 11 > 2006.10.02. Aladar 10 > 2006.10.02. Cecil 9 > 2006.10.02. Dezso 8 > 2006.10.02. Ferenc 7 > 2006.10.03. Bela 6 > 2006.10.03. Aladar 5 > 2006.10.03. Cecil 4 > 2006.10.03. Dezso 3 > 2006.10.03. Jozef 2 > The first column is a date, the second is a name, the third is the > number of votes that the name received on that day. > I would like to select the 3 (or 10) names with the most votes for > each day. > Any suggestions on how can it be done easily? It'd be easy with windowing functions, but unfortunately we don't have those... SELECT * FROM (SELECT DISTINCT date FROM table) AS dates , (SELECT date, name, votes FROM table WHERE table.date = dates.date ORDER BY votes DESC LIMIT 3 ) ; Note that this has to scan the table twice (well, the second subquery will likely use an index on date). If you have another table that has the dates in it already, you can use that instead of the first subquery. If you know that every day has a row, you could also replace the first subquery with a generate_series(). -- Jim Nasby jim@nasby.net EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)