X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6C2F2D8117 for ; Wed, 12 Oct 2005 18:54:51 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 45982-10 for ; Wed, 12 Oct 2005 21:54:48 +0000 (GMT) Received: from stark.xeocode.com (stark.xeocode.com [216.58.44.227]) by svr1.postgresql.org (Postfix) with ESMTP id 5B38CD8115 for ; Wed, 12 Oct 2005 18:54:49 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1EPoYT-0000J9-00; Wed, 12 Oct 2005 17:54:33 -0400 To: Stephan Szabo Cc: Rick Schumeyer , pgsql-sql@postgresql.org Subject: Re: pg, mysql comparison with "group by" clause References: <001001c5cea8$77cabc90$0300a8c0@dell8200> <20051011142658.C54076@megazone.bigpanda.com> In-Reply-To: <20051011142658.C54076@megazone.bigpanda.com> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 12 Oct 2005 17:54:33 -0400 Message-ID: <87br1u1jvq.fsf@stark.xeocode.com> Lines: 34 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.011 required=5 tests=[AWL=0.011] X-Spam-Level: X-Archive-Number: 200510/108 X-Sequence-Number: 22961 Stephan Szabo writes: > On Tue, 11 Oct 2005, Rick Schumeyer wrote: > > > I'm not sure what I was thinking, but I tried the following query in pg: > > > > SELECT * FROM t GROUP BY state; > > > > pg returns an error. > > > > Mysql, OTOH, returns the first row for each state. (The first row with > > "AK", the first row with "PA", etc.) > > > > I'm no SQL expert, but it seems to me that the pg behavior is correct, and > > the mysql result is just weird. Am I correct? > > In your case, it sounds like the mysql result is wrong. I believe SQL99 > would allow it if the other columns were functionally dependant upon state > (as there'd by definition only be one value for the other columns per > group). I believe this is a documented feature. MySQL treats "select a,b from t group by a" equivalently to Postgres's "select distinct on (a) a,b from t" I suppose "equivalent" isn't quite true. It's more general since it allows aggregate functions as well. The equivalently general Postgres syntax is to have a first() aggregate function and do "select a,first(b) from t group by a". I'm sure it's very convenient. -- greg