pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Metz <tmetz@gsf.de>
To: pgsql-sql@hub.org
Subject: SELECT DISTINCT ON ... ORDER BY ...
Date: Thu, 28 Jan 1999 14:01:03 +0100
Message-ID: <36B05F8F.DFB94ED8@gsf.de> (raw)

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 id, name FROM test;
produces:

ID     NAME
-----------------
1      Alex
2      Oliver


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. 

If the table is as follows:

ID     NAME
-----------------
2      Oliver
2      Alex
1      Thomas
1      Fenella


SELECT DISTINCT ON id id, name FROM test;
produces:

ID     NAME
-----------------
1      Thomas
2      Oliver


SELECT DISTINCT ON id, name FROM test ORDER BY name;
produces:

ID     NAME
-----------------
2      Alex
1      Fenella
2      Oliver
1      Thomas

What seems to happen is that from the sorted table, duplicate id's are
eliminated only if they are grouped. If there is no explicit sorting I
assume the DISTINCT performs an implicit sorting on id and then
eliminates records correctly. Is that the correct behaviour? Is there
another (simple) way of getting the results I want?

I am still using version 6.3

tm
-- 
Thomas Metz
GSF - National Research Center for Environment and Health
Institute of Mammalian Genetics



view thread (9+ messages)  latest in thread

Message-ID: <36B05F8F.DFB94ED8@gsf.de>
Permalink:  ../36B05F8F.DFB94ED8@gsf.de/
Also on:    postgresql.org/message-id/36B05F8F.DFB94ED8@gsf.de

 · 

reply

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: tmetz@gsf.de, pgsql-sql@hub.org
  Subject: Re: SELECT DISTINCT ON ... ORDER BY ...
  In-Reply-To: <36B05F8F.DFB94ED8@gsf.de>
  References: <36B05F8F.DFB94ED8@gsf.de>

* 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