From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-hackers@postgreSQL.org
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Date: Wed, 26 Jan 2000 13:40:54 -0500
Message-ID: <18389.948912054@sss.pgh.pa.us> (raw)
In-Reply-To: <Pine.LNX.4.21.0001260143120.9535-100000@localhost.localdomain>
References: <Pine.LNX.4.21.0001260143120.9535-100000@localhost.localdomain>
Peter Eisentraut <peter_e@gmx.net> writes:
> Our documents say that DISTINCT ON is equivalent to GROUP BY. I still
> don't see why that wouldn't be true. You can always rewrite
> select distinct on a a,b from test
> as
> select a, xxx(b) from test group by a
> where xxx is some aggregate function (presumably min or max).
Not really. Look at Julian's example. He can't rewrite as
select a, min(b), min(c) from test group by a
because the idea is to get the c that corresponds to the min b.
If you do it with two independent aggregates then the b and c
you get back may be from different tuples.
I could imagine fixing this with a two-input aggregate, say
select a, min(b), keyofmin(b, c) from test group by a
where keyofmin is defined to return the c associated with the min b.
But that'd be a pain to implement, first because we have no support
for multi-argument aggregates, and second because you'd need a ton
of separate keyofmin implementations for the cross-product of the
data types you might want to deal with. So this is nearly as
klugy as the SELECT DISTINCT ON approach --- and not any more
standard, either.
regards, tom lane
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: tgl@sss.pgh.pa.us, peter_e@gmx.net, pgsql-hackers@postgreSQL.org
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
In-Reply-To: <18389.948912054@sss.pgh.pa.us>
References: <Pine.LNX.4.21.0001260143120.9535-100000@localhost.localdomain> <18389.948912054@sss.pgh.pa.us>
* 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