Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 66F009FB3BA for ; Sat, 17 Feb 2007 14:40:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 33333-08 for ; Sat, 17 Feb 2007 14:40:00 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from zenon.apartia.fr (zenon.apartia.fr [82.66.93.83]) by postgresql.org (Postfix) with ESMTP id 538209FB3B1 for ; Sat, 17 Feb 2007 14:40:00 -0400 (AST) Received: from styx.apartia.fr (styx.apartia.fr [10.0.3.109]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "styx.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by zenon.apartia.fr (Postfix) with ESMTP id D693CF4B8A32F for ; Sat, 17 Feb 2007 19:39:58 +0100 (CET) Received: by styx.apartia.fr (Postfix, from userid 1000) id 20078B2AE50B; Sat, 17 Feb 2007 19:40:00 +0100 (CET) Date: Sat, 17 Feb 2007 19:40:00 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: sub-limiting a query Message-ID: <20070217183959.GA29756@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20070217125634.GA20589@apartia.fr> <45D747B4.5050900@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <45D747B4.5050900@gmail.com> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.509 tagged_above=0 required=5 tests=AWL, BAYES_50, RCVD_IN_NJABL_DUL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: * X-Archive-Number: 200702/188 X-Sequence-Number: 27522 On Sat, Feb 17, 2007 at 07:21:40PM +0100, M.P.Dankoor wrote: > Louis-David Mitterrand wrote: > > > I thought of another solution, actually it's of those top n query tricks > that I picked up somewhere, can't remember > where. > Assuming that your table is called shows, the following query should > give you the results you want (hope so) > > SELECT * > FROM shows a > WHERE 3 > (SELECT COUNT(*) > FROM shows b > WHERE b.created_on >= a.created_on > and a.show_type = b.show_type) This is stunning and it works! I can barely understand the query: it's so terse it hurts :) /me goes back studying it Thanks a lot!