Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id ED2B59F9801 for ; Mon, 27 Aug 2007 15:13:09 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 05944-07 for ; Mon, 27 Aug 2007 15:13:07 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mtao03.charter.net (mtao03.charter.net [209.225.8.188]) by postgresql.org (Postfix) with ESMTP id BF0889F97F3 for ; Mon, 27 Aug 2007 15:13:06 -0300 (ADT) Received: from aa03.charter.net ([10.20.200.155]) by mtao03.charter.net (InterMail vM.7.08.02.00 201-2186-121-20061213) with ESMTP id <20070827181257.QQZW10962.mtao03.charter.net@aa03.charter.net>; Mon, 27 Aug 2007 14:12:57 -0400 Received: from [10.0.1.2] (really [66.188.165.91]) by aa03.charter.net with ESMTP id <20070827181257.QTWR7864.aa03.charter.net@[10.0.1.2]>; Mon, 27 Aug 2007 14:12:57 -0400 In-Reply-To: <4CB5D209-C0CC-466E-A967-E766D6AD799B@joelrichard.com> References: <46D2FD9F.3020000@gmx.net> <20070827125102.e52b43de.darcy@druid.net> <4CB5D209-C0CC-466E-A967-E766D6AD799B@joelrichard.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <60F656A0-805D-4C3B-BCA5-742AC6C866E0@seespotcode.net> Cc: "D'Arcy J.M. Cain" , Claudia Kosny , sql pgsql Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: fetch first rows of grouped data Date: Mon, 27 Aug 2007 13:12:48 -0500 To: Joel Richard X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/224 X-Sequence-Number: 29162 [Please don't top post as it makes the discussion more difficult to follow.] On Aug 27, 2007, at 12:34 , Joel Richard wrote: > > On Aug 27, 2007, at 12:51 PM, D'Arcy J.M. Cain wrote: > >> On Mon, 27 Aug 2007 18:36:47 +0200 >> Claudia Kosny wrote: >>> I have a list of events that take place in a certain city at a >>> certain >>> date. Now I would like to have the first two (ordered by date) >>> events >>> for each city. >>> >>> Is there a way to do this with one query? >>> I am using PostgreSQL 7.4. >> >> I believe you are looking for the LIMIT keyword. Check the docs >> on the >> web site. > That's what I thought at first, but this is not the case. She's > looking for the first two dates in -each- city in the table. I > initially thought that this could be accomplished with GROUP BY and > LIMIT, but GROUP BY can only give you the first date for each city, > not the first two dates for each city. > > So far, I haven't come up with any clever ideas. I'm not sure this > can be done in SQL. > > --Joel > I can't think of an easy way to do it, but certainly can do it in SQL using correlated subqueries, something like select city, event, event_date, ( select count(event) from events i where i.city = o.city and i.event_date < o.event_date and event_date > current_date -- make sure they're future events ) as nearness_rank from events o where event_date > current_date -- make sure they're future events having nearness_rank <= 1; Note that this can potentially show more than 2 events if the most recent upcoming events "tie" (have the same event_date). Michael Glaesemann grzm seespotcode net