Re: fetch first rows of grouped data - Mailing list pgsql-sql

From D'Arcy J.M. Cain
Subject Re: fetch first rows of grouped data
Date
Msg-id 20070827141552.21ec3a54.darcy@druid.net
Whole thread Raw
In response to Re: fetch first rows of grouped data  (Joel Richard <postgres@joelrichard.com>)
List pgsql-sql
On Mon, 27 Aug 2007 13:34:09 -0400
Joel Richard <postgres@joelrichard.com> wrote:
> 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.

Yes, you are correct.  My mistake.

> So far, I haven't come up with any clever ideas. I'm not sure this  
> can be done in SQL.

Well, I can think of an SQL only method involving a temporary table but
it would not be a single query:
- Select into temp table where date is max.- Select from temp table union with main table where date is max and
is not in temp table.

-- 
D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.


pgsql-sql by date:

Previous
From: Michael Glaesemann
Date:
Subject: Re: fetch first rows of grouped data
Next
From: Michael Glaesemann
Date:
Subject: Re: fetch first rows of grouped data