Re: [SQL] DISTINCT ON: speak now or forever hold your peace

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Julian Scarfe <jscarfe(at)callnetuk(dot)com>
Cc: pgsql-hackers(at)postgreSQL(dot)org, pgsql-sql <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] DISTINCT ON: speak now or forever hold your peace
Date: 2000-01-27 18:33:16
Message-ID: 5844.948997996@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers 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 accept
SELECT 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff MacDonald <jeff@pgsql.com> 2000-01-27 18:43:22 Spinlock error
Previous Message The Hermit Hacker 2000-01-27 18:05:00 Re: [HACKERS] CVS problem

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Wilson 2000-01-27 20:32:39 transaction aborted
Previous Message moreno 2000-01-27 18:20:33 Someone can help please? SELECT problems