Re: [SQL] Re: [HACKERS] Re: SELECT DISTINCT ON ... ORDER BY ...

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: pgsql-sql(at)postgreSQL(dot)org, pgsql-hackers(at)postgreSQL(dot)org
Cc: pgsql-sql(at)hub(dot)org, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [SQL] Re: [HACKERS] Re: SELECT DISTINCT ON ... ORDER BY ...
Date: 1999-02-02 15:57:08
Message-ID: l0311070ab2dccf18e2be@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Oleg Broytmann 1999-02-02 16:19:13 Re: [HACKERS] New patch (was: tough bug)
Previous Message The Hermit Hacker 1999-02-02 15:51:08 Re: [HACKERS] Postgres Speed or lack thereof

Browse pgsql-sql by date

  From Date Subject
Next Message Jackson, DeJuan 1999-02-02 18:17:45 RE: [GENERAL] Dashed if I can work this out. Help needed copying substring to another field
Previous Message jose' soares 1999-02-02 15:25:19 Re: [SQL] Re: [HACKERS] Re: SELECT DISTINCT ON ... ORDER BY ...