X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B25CF3A48D1 for ; Thu, 4 Nov 2004 17:32:16 +0000 (GMT) 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 49053-04 for ; Thu, 4 Nov 2004 17:31:58 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id A4C1F3A3E73 for ; Thu, 4 Nov 2004 17:31:57 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iA4HVrK5014048 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 4 Nov 2004 10:31:55 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iA4HVrmi020854; Thu, 4 Nov 2004 10:31:53 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iA4HVrEj020853; Thu, 4 Nov 2004 10:31:53 -0700 (MST) (envelope-from mfuhr) Date: Thu, 4 Nov 2004 10:31:53 -0700 From: Michael Fuhr To: "Michael L. Hostbaek" , pgsql-sql@postgresql.org Subject: Re: Group by and aggregates Message-ID: <20041104173153.GA20769@winnie.fuhr.org> References: <20041104165430.GD94770@mich2.itxmarket.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041104165430.GD94770@mich2.itxmarket.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/36 X-Sequence-Number: 19355 On Thu, Nov 04, 2004 at 05:54:30PM +0100, Michael L. Hostbaek wrote: > some_id partno status cmup qty > 1 test1 stock 10.00 15 > 2 test2 incoming 12.00 10 > 3 test1 incoming 15.00 60 > 4 test1 incoming 14.00 11 > > My SQL select statement will then group together partno, status and > aggregate sum(qty) and max(cmup). This is all good and nice. It would be helpful to see the exact query you're running. Based on the query output you posted below, I'd guess your query looks like this: SELECT partno, status, MAX(cmup) AS cmup, SUM(qty) AS qty FROM my_table GROUP BY partno, status ORDER BY partno, status DESC; > My result will look something like this: > > partno status cmup qty > test1 stock 10.00 15 > test1 incoming 15.00 71 > test2 incoming 12.00 10 > > Now, I need the first line to say "15.00" in the cmup field. That is, > stock and incoming are obviously not being grouped, but since it's the > same partno I'd like somehow to show the highest cmup. The query I posted above duplicates this output exactly. The cmup field in the first record is 10.00 because that's the maximum value of cmup where partno='test1' and status='stock', which is how I (and presumably you) specified the grouping to work with GROUP BY. Perhaps you want to group only by partno and not by status: SELECT partno, MAX(cmup) AS cmup, SUM(qty) AS qty FROM my_table GROUP BY partno ORDER BY partno; partno | cmup | qty --------+-------+----- test1 | 15.00 | 86 test2 | 12.00 | 10 If that's not what you want, then please post the exact output you're looking for. If you want to include the status field, then please explain why a record for 'test1' and 'stock' should have a MAX(cmup) of 15.00. -- Michael Fuhr http://www.fuhr.org/~mfuhr/