Received: from mx99.openu.ac.il (arbel.openu.ac.il [147.233.32.4]) by hub.org (8.9.2/8.9.1) with ESMTP id KAA94900 for ; Tue, 2 Feb 1999 10:56:52 -0500 (EST) (envelope-from herouth@oumail.openu.ac.il) Received: from talmon.openu.ac.il (talmon.openu.ac.il [147.233.32.11]) by mx99.openu.ac.il (Postfix) with ESMTP for id 821DD4B50B; Tue, 2 Feb 1999 17:56:51 +0200 (IST) Received: from [147.233.159.109] (mac-herouth.pc.openu.ac.il) by talmon.openu.ac.il (Netscape Messaging Server 3.54) with ESMTP id AAA5FA7; Tue, 2 Feb 1999 17:57:59 +0200 X-Sender: herouth@shaked.cc.openu.ac.il Message-Id: In-Reply-To: <36B718DF.53BE37EF@bo.nettuno.it> References: <199902011640.QAA20774@linda.lfix.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Date: Tue, 2 Feb 1999 17:57:08 +0200 To: pgsql-sql@postgreSQL.org, pgsql-hackers@postgreSQL.org From: Herouth Maoz Subject: Re: [SQL] Re: [HACKERS] Re: SELECT DISTINCT ON ... ORDER BY ... Cc: pgsql-sql@hub.org, pgsql-hackers@postgreSQL.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by hub.org id KAA94903 At 17:25 +0200 on 02/02/1999, jose' soares wrote: > This gives the same results: > > junk=> select cognome, nome, via from membri where cap = '41010' > group by cognome; > cognome|nome      |via > -------+----------+-------------------------- > FIORANI|ELISABETTA|VIA PRETI PARTIGIANI, 63 > GOZZI  |LILIANA   |VIA MAGNAGHI, 39 > RUSSO  |DAVIDE    |STRADA CORLETTO SUD, 194/1 > > This is very interesting and useful, I thought it wasn't possible. Seems >that standard allows only the "order by" column(s) > and the aggregate function(s) on target list. > I tried the same query on Informix, also on Ocelot but it gives me an error. And with good reason, too. The above query has the same drawback as the "select distinct on", which is: it does not fully specify which value should be selected for the "nome" and "via" fields. Thus, running this same query on a table that has the same data but was, for example, filled in a different order, gives a different result. That's bad, because order should not make a difference for output. Tables are taken to be unordered sets. If you want to have a representative of the "nome" and "via" fields, and it doesn't matter which representative, then min(nome) or max(nome) should do the trick. And this query (select cognome, min(nome), min(via)... group by cognome) should give you the same result on all databases, no matter which rows were inserted first. If it was up to me, I wouldn't use the above form, and frankly, I am surprised the Postgres allows this. Herouth -- Herouth Maoz, Internet developer. Open University of Israel - Telem project http://telem.openu.ac.il/~herutma