Re: Why must SELECT DISTINCT, ORDER BY expressions must appear in target list? - Mailing list pgsql-general

From Greg Stark
Subject Re: Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?
Date
Msg-id 87wuk1h2s5.fsf@stark.dyndns.tv
Whole thread Raw
In response to Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?  (Dave Smith <dave.smith@candata.com>)
List 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 by date:

Previous
From: Greg Stark
Date:
Subject: Re: CREATE TABLE with a name derived from a string
Next
From: Bruce Momjian
Date:
Subject: Re: Dropping column silently kills multi-coumn index (was