Received: from sss2.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.9.3/8.9.3) with ESMTP id NAA38420; Wed, 26 Jan 2000 13:40:59 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss2.sss.pgh.pa.us (8.9.3/8.9.3) with ESMTP id NAA18392; Wed, 26 Jan 2000 13:40:54 -0500 (EST) To: Peter Eisentraut cc: pgsql-hackers@postgreSQL.org, pgsql-sql@postgreSQL.org Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace In-reply-to: References: Comments: In-reply-to Peter Eisentraut message dated "Wed, 26 Jan 2000 19:34:54 +0100" Date: Wed, 26 Jan 2000 13:40:54 -0500 Message-ID: <18389.948912054@sss.pgh.pa.us> From: Tom Lane Peter Eisentraut 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