Re: Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?
Дата
Msg-id 87wuk1h2s5.fsf@stark.dyndns.tv
обсуждение исходный текст
Ответ на Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?  (Dave Smith <dave.smith@candata.com>)
Список pgsql-general
Dave Smith <dave.smith@candata.com> writes:

> For this query ..
>
> SELECT DISTINCT t0_s.rec_num
> ...
> ORDER BY t0_s.date_of_request DESC

Well, what should the database do if two different records with the same
"rec_num" have different "date_of_request"s? You may know that won't happen
but the database doesn't know that.

Postgres doesn't really care what columns you're looking at, just that
date_of_request be part of the distinct criteria which for "distinct" means
being in the select list. If you use the equivalent GROUP BY instead you could
leave it off the select list but you would still have to have it in the GROUP
BY clause.

Note though that in 7.3 the distinct can be more efficient than the group by
version. In 7.4 the group by might be faster.

--
greg

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

Предыдущее
От: Greg Stark
Дата:
Сообщение: Re: CREATE TABLE with a name derived from a string
Следующее
От: Bruce Momjian
Дата:
Сообщение: Re: Dropping column silently kills multi-coumn index (was