Re: [SQL] SELECT DISTINCT ON ... ORDER BY ...

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: Thomas Metz <tmetz(at)gsf(dot)de>
Cc: pgsql-sql(at)hub(dot)org
Subject: Re: [SQL] SELECT DISTINCT ON ... ORDER BY ...
Date: 1999-01-28 14:55:27
Message-ID: 199901281455.OAA23605@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thomas Metz wrote:
>I have the following problem:
>
>Assuming the table TEST as follows:
>
>ID NAME
>-----------------
>1 Alex
>2 Oliver
>1 Thomas
>2 Fenella
...
>SELECT DISTINCT ON id, name FROM test ORDER BY name;
>produces:
>
>ID NAME
>-----------------
>1 Alex
>2 Fenella
>1 Thomas
>
>I would have expected only two rows in both queries. I don't care which
>names actually appear in the output as long as they are sorted, but
>there should no longer be duplicate id's.

I looked at the documentation for SELECT and found that DISTINCT ON is
mentioned but not explained; there are also some other parts of that
documentation that need expanding or correcting, so I'm doing that at
the moment.

Is there any intended difference between DISTINCT ON and GROUP BY? (I
realise from this thread that there is an actual difference!)

Am I correct in saying that DISTINCT ON is not part of SQL92?

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP key from public servers; key ID 32B8FAA1
========================================
"Many are the afflictions of the righteous; but the
LORD delivereth him out of them all."
Psalm 34:19

Browse pgsql-sql by date

  From Date Subject
Next Message Tim Perdue 1999-01-28 18:46:02 Auto-Vacuum?
Previous Message Stanimir Stanev 1999-01-28 14:41:58 ERROR: user "test1" is not allowed to create/destroy databases