From: Chris Bitmead <chris.bitmead@bigfoot.com>
To: pgsql-sql@postgresql.org <pgsql-sql@postgreSQL.org>
Subject: Confusion about DISTINCT.
Date: Sun, 25 Apr 1999 09:23:20 +0000
Message-ID: <3722DF08.D8E19160@bigfoot.com> (raw)
References: <371F2ED6.29FD9251@scigen.co.uk>
I'm a bit confused about DISTINCT. Firstly, what does DISTINCT mean when
you don't use "ON". Like this...
SELECT DISTINCT category.image FROM story, category* WHERE
story.category = category.oid ORDER BY datetime DESC;
The DISTINCT here has no effect on the output other than changing the
ordering. There are duplicates!
2nd question: Why does the following query result in duplicates even
though I use DISTINCT ON? If I change the ORDER BY to be on image, then
there are no duplicates but that isn't what I want. I want the time to
be the sort order because I want the X most recent images but only
unique ones. Is this a bug? It certainly seems wierd that DISTINCT would
return duplicates. Why should it be up to the user to order the output
with reference to the DISTINCT clause? Shouldn't the database take care
of that?
...
SELECT DISTINCT ON image category.image FROM story, category* WHERE
story.category = category.oid ORDER BY datetime DESC;
image
--------------------
/icon/canon.gif
/icon/arca-swiss.gif
/icon/canon.gif
/icon/hasselblad.gif
/icon/nikon.gif
/icon/olympus.gif
(6 rows)
--
Chris Bitmead
http://www.bigfoot.com/~chris.bitmead
mailto:chris.bitmead@bigfoot.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: chris.bitmead@bigfoot.com
Subject: Re: Confusion about DISTINCT.
In-Reply-To: <3722DF08.D8E19160@bigfoot.com>
References: <371F2ED6.29FD9251@scigen.co.uk> <3722DF08.D8E19160@bigfoot.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox