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

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why must SELECT DISTINCT, ORDER BY expressions must appear in target list?
Date: 2003-02-15 17:28:10
Message-ID: 87wuk1h2s5.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dave Smith <dave(dot)smith(at)candata(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2003-02-15 18:21:32 Re: Dropping column silently kills multi-coumn index (was
Previous Message Greg Stark 2003-02-15 17:22:23 Re: CREATE TABLE with a name derived from a string