Re: fetch first rows of grouped data

From: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
To: Joel Richard <postgres(at)joelrichard(dot)com>
Cc: Claudia Kosny <ckosny(at)gmx(dot)net>, sql pgsql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: fetch first rows of grouped data
Date: 2007-08-27 18:15:52
Message-ID: 20070827141552.21ec3a54.darcy@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, 27 Aug 2007 13:34:09 -0400
Joel Richard <postgres(at)joelrichard(dot)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(at)druid(dot)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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2007-08-27 18:20:18 Re: fetch first rows of grouped data
Previous Message Michael Glaesemann 2007-08-27 18:12:48 Re: fetch first rows of grouped data