Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id DC74F9FB6C4 for ; Sat, 17 Feb 2007 14:22:54 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 32817-09 for ; Sat, 17 Feb 2007 14:22:45 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from smtp-vbr4.xs4all.nl (smtp-vbr4.xs4all.nl [194.109.24.24]) by postgresql.org (Postfix) with ESMTP id A4C1E9FB618 for ; Sat, 17 Feb 2007 14:22:51 -0400 (AST) Received: from [192.168.1.3] (213-84-236-203.adsl.xs4all.nl [213.84.236.203]) (authenticated bits=0) by smtp-vbr4.xs4all.nl (8.13.8/8.13.8) with ESMTP id l1HIMo8i078938 for ; Sat, 17 Feb 2007 19:22:50 +0100 (CET) (envelope-from m.p.dankoor@gmail.com) Message-ID: <45D747B4.5050900@gmail.com> Date: Sat, 17 Feb 2007 19:21:40 +0100 From: "M.P.Dankoor" User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: sub-limiting a query References: <20070217125634.GA20589@apartia.fr> In-Reply-To: <20070217125634.GA20589@apartia.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by XS4ALL Virus Scanner X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.853 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_NEUTRAL X-Spam-Level: X-Archive-Number: 200702/187 X-Sequence-Number: 27521 Louis-David Mitterrand wrote: > Hello, > > I've got a table of shows with different types (movie, theater, > ballet,etc.) and I am looking for a select that can return the 10 last > entered shows AND at most 2 of each type. Is that possible in one query? > > The table looks basically like: > > created_on | timestamp without time zone > show_name | text > id_show | integer > show_type | text > id_show_subtype | integer > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster > > 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) Mario