Re: [SQL] DISTINCT ON: speak now or forever hold your peace - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] DISTINCT ON: speak now or forever hold your peace
Date
Msg-id 5844.948997996@sss.pgh.pa.us
Whole thread Raw
In response to Re: [SQL] DISTINCT ON: speak now or forever hold your peace  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
I wrote:
> To fix this we need some kind of syntactic separator.  The cleanest
> idea that comes to my mind is to require parentheses around the ON
> target:
>         SELECT DISTINCT ON (expression) target1, ...
> One immediate advantage of allowing an expression is that you can do
> distinct-on-two-fields in a rather klugy way, eg
>         SELECT DISTINCT ON (field1 || ' ' || field2) ...
> We might someday extend it to allow multiple DISTINCT fields, eg,
>         SELECT DISTINCT ON (expr1 [, expr2 ...]) target1, ...
> but I'm not promising to do that now (unless it's really easy ;-)).

FYI, I have committed this change.  It did turn out to be just as easy
to support multiple DISTINCT ON expressions, so 7.0 will acceptSELECT DISTINCT ON (expr1 [, expr2 ...]) target1, ...

> What I'd prefer to do is put in an
> error check that says "if you use both DISTINCT ON and ORDER BY, then
> the DISTINCT ON expression must be the first ORDER BY item".

More generally, if you use both then the DISTINCT and ORDER lists must
match until one or the other runs out.
        regards, tom lane


pgsql-sql by date:

Previous
From: moreno
Date:
Subject: Someone can help please? SELECT problems
Next
From: Mark Wilson
Date:
Subject: transaction aborted