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 54F5C3A48F9 for ; Thu, 4 Nov 2004 17:25:18 +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 44677-10 for ; Thu, 4 Nov 2004 17:25:14 +0000 (GMT) Received: from outmail.freedom2surf.net (outmail1.freedom2surf.net [194.106.33.237]) by svr1.postgresql.org (Postfix) with ESMTP id C963B3A488A for ; Thu, 4 Nov 2004 17:25:11 +0000 (GMT) Received: from sam.samason.me.uk (sam.samason.me.uk [195.137.31.118]) by outmail.freedom2surf.net (8.12.10/8.12.10) with ESMTP id iA4HP8nF024963 for ; Thu, 4 Nov 2004 17:25:09 GMT Received: by sam.samason.me.uk (Postfix, from userid 1000) id 1DE65636B9; Thu, 4 Nov 2004 17:25:08 +0000 (GMT) Date: Thu, 4 Nov 2004 17:25:08 +0000 From: Sam Mason To: pgsql-sql@postgresql.org Subject: Re: Group by and aggregates Message-ID: <20041104172507.GG25775@sam.samason.me.uk> Mail-Followup-To: pgsql-sql@postgresql.org References: <20041104165430.GD94770@mich2.itxmarket.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: <20041104165430.GD94770@mich2.itxmarket.com> User-Agent: Mutt/1.5.6i 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/63 X-Sequence-Number: 19382 Michael L. Hostbaek wrote: >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. Is there some >black SQL voodoo that'll achieve this ? I think you need to join the table back on itself to get the total for each part. Not a very impressive incantation, but I'd do it in SQL like this: SELECT x.partno, x.status, y.tot, sum(x.qty) FROM my_table x, ( SELECT partno, MAX(cmup) as tot FROM my_table GROUP BY partno) y WHERE x.partno = y.partno GROUP BY x.partno, x.status, y.tot Cheers, Sam