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

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [SQL] Finding the "most recent" rows
Дата
Msg-id 20211.924828306@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: [SQL] Finding the "most recent" rows  (Chris Bitmead <chris.bitmead@bigfoot.com>)
Список pgsql-sql
Chris Bitmead <chris.bitmead@bigfoot.com> writes:
> Clever. But why doesn't this work....

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

A subselect used in an expression has to return exactly one value;
yours will return as many tuples as there are distinct titles.

I think you meant

select title, summary, time from story t where time = (select
max(s.time) from story s WHERE s.title = t.title);               

Here the subselect should give a single result each time it's
executed.  Unfortunately, it's gonna be executed once for each
tuple scanned by the outer select :-(
        regards, tom lane


В списке pgsql-sql по дате отправления:

Предыдущее
От: Chris Bitmead
Дата:
Сообщение: Re: [SQL] SELECT TOP X -- part 2 -- parse error?
Следующее
От: Chris Bitmead
Дата:
Сообщение: Re: [SQL] Finding the "most recent" rows