Re: [SQL] Finding the "most recent" rows

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Chris Bitmead <chris(dot)bitmead(at)bigfoot(dot)com>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] Finding the "most recent" rows
Date: 1999-04-29 10:04:54
Message-ID: l03130300b34ddda65755@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 07:39 +0300 on 23/04/1999, Chris Bitmead wrote:

> httpd=> select title, summary, time from story t where time IN (select
> max(s.time) from story s GROUP BY s.title);
> ERROR: parser: Subselect has too many or too few fields.

Of course it does. Since you group by a field which is not in the select
list, Postgres adds it silently to the fields to be selected. Thus the
subselect has two fields in each row, not just a single time field.

I think the proper syntax by the standard would be something like:

SELECT title, summary, time
FROM story t
WHERE (time, title) IN (
SELECT s.title, max( s.time )
FROM story s
GROUP BY s.title
);

But I'm not sure Postgres even supports this format (of comparing against
several fields).

In any case, the best would be to select just one tuple in the subselect
and have it return only the time, by constraining the title. But
syntactically, the above is what you were trying to do.

Anyway, Postgres adds the group field to the query, which the standard
requires and common practice doesn't. I think perhaps after doing the
grouping, Postgres should drop that field, since it wasn't originally
requested.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Herouth Maoz 1999-04-29 10:28:38 Re: [SQL] Finding the "most recent" rows
Previous Message Mark Jewiss 1999-04-29 09:24:46 Re: [SQL] LIMIT