Confusion about DISTINCT.
От | Chris Bitmead |
---|---|
Тема | Confusion about DISTINCT. |
Дата | |
Msg-id | 3722DF08.D8E19160@bigfoot.com обсуждение исходный текст |
Ответ на | Finding the "most recent" rows (Julian Scarfe <jas1@scigen.co.uk>) |
Ответы |
Re: [SQL] Confusion about DISTINCT.
|
Список | 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 по дате отправления: