Confusion about DISTINCT.

From: Chris Bitmead <chris(dot)bitmead(at)bigfoot(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgreSQL(dot)org>
Subject: Confusion about DISTINCT.
Date: 1999-04-25 09:23:20
Message-ID: 3722DF08.D8E19160@bigfoot.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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(dot)bitmead(at)bigfoot(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-04-25 16:22:43 Re: [SQL] Confusion about DISTINCT.
Previous Message Nigel Tamplin 1999-04-25 08:10:07 Relating 1 table to another.