Confusion about DISTINCT. - Mailing list pgsql-sql

From Chris Bitmead
Subject Confusion about DISTINCT.
Date
Msg-id 3722DF08.D8E19160@bigfoot.com
Whole thread Raw
In response to Finding the "most recent" rows  (Julian Scarfe <jas1@scigen.co.uk>)
Responses Re: [SQL] Confusion about DISTINCT.
List 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.bitmead@bigfoot.com


pgsql-sql by date:

Previous
From: Chris Bitmead
Date:
Subject: Percentages?
Next
From: "Nigel Tamplin"
Date:
Subject: Relating 1 table to another.