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 9B4C8D7CDB for ; Thu, 13 Oct 2005 15:37:54 -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 78938-01 for ; Thu, 13 Oct 2005 18:37:51 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id ACEFBD77B9 for ; Thu, 13 Oct 2005 15:37:51 -0300 (ADT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 13 Oct 2005 13:37:52 -0500 Subject: Re: pg, mysql comparison with "group by" clause From: Scott Marlowe To: Greg Stark Cc: Stephan Szabo , Rick Schumeyer , pgsql-sql@postgresql.org In-Reply-To: <878xwxi87h.fsf@stark.xeocode.com> References: <001001c5cea8$77cabc90$0300a8c0@dell8200> <20051011142658.C54076@megazone.bigpanda.com> <87br1u1jvq.fsf@stark.xeocode.com> <1129155875.29961.168.camel@state.g2switchworks.com> <87zmpez0au.fsf@stark.xeocode.com> <1129219046.29961.184.camel@state.g2switchworks.com> <878xwxi87h.fsf@stark.xeocode.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1129228672.29961.214.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Thu, 13 Oct 2005 13:37:52 -0500 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 required=5 tests=[AWL=0.007] X-Spam-Level: X-Archive-Number: 200510/144 X-Sequence-Number: 22997 On Thu, 2005-10-13 at 13:26, Greg Stark wrote: > Scott Marlowe writes: > > > Sorry, but it's worse than that. It is quite possible that two people > > could run this query at the same time and get different data from the > > same set and the same point in time. That shouldn't happen accidentally > > in SQL, you should know it's coming. > > I'm pretty unsympathetic to the "we should make a language less powerful and > more awkward because someone might use it wrong" argument. I'm in favor of getting the right answer. If my database can't do that, then it's not worth the bits to download it. But I'm funny that way. > > > In standard SQL you have to > > > write GROUP BY ... and list every single column you need from the master > > > table. Forcing the database to do a lot of redundant comparisons and sort on > > > uselessly long keys where in fact you only really need it to sort and group by > > > the primary key. > > > > But again, you're getting whatever row the database feels like giving > > you. A use of a simple, stupid aggregate like an any() aggregate would > > be fine here, and wouldn't require a lot of overhead, and would meet the > > SQL spec. > > Great, so I have a user table with, oh, say, 40 columns. And I want to return > all those columns plus their current account balance in a single query. > > The syntax under discussion would be: > > select user.*, sum(money) from user join user_money using (user_id) group by user_id > You would prefer: > > select user_id, > any(username) as username, any(firstname) as firstname, > any(lastname) as lastname, any(address) as address, > any(city) as city, any(street) as street, any(phone) as phone, > any(last_update) as last_update, any(last_login) as last_login, > any(referrer_id) as referrer_id, any(register_date) as register_date, > ... > sum(money) as balance, > count(money) as num_txns > from user join user_money using (user_id) group by user_id I's select the SINGLE entries from a child table that matched the parent id and add the sum(money) to it. Then, there'd be no need for aggregate functions on those fields, or inaccurate / possibly random data. But I'm funny that way. > Having a safeties is fine but when I have to disengage the safety for every > single column it starts to get more than a little annoying. > > Note that you cannot write the above as a subquery since there are two > aggregates. You could write it as a join against a view but don't expect to > get the same plans from Postgres for that. I'd just write is a big join. Again, getting the right answer is important to me.