Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-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

pgsql-hackers by date

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

pgsql-sql by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group