X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id C3C3E9FA218 for ; Fri, 26 May 2006 13:23:24 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 69448-01 for ; Fri, 26 May 2006 13:23:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mx1.ipf.is (benz.ipf.is [85.197.192.156]) by postgresql.org (Postfix) with ESMTP id 8D07D9FA12E for ; Fri, 26 May 2006 13:23:17 -0300 (ADT) Received: (qmail 12374 invoked by uid 1010); 26 May 2006 16:21:18 +0000 Received: from 85.197.216.186 by benz (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.88/1235. spamassassin: 3.1.1. perlscan: 1.25-st-qms. Clear:RC:1(85.197.216.186):SA:0(-4.2/4.5):. Processed in 0.7405 secs); 26 May 2006 16:21:18 -0000 X-Antivirus-IPF-Mail-From: gnari@hive.is via benz X-Antivirus-IPF: 1.25-st-qms (Clear:RC:1(85.197.216.186):SA:0(-4.2/4.5):. Processed in 0.7405 secs Process 12357) Received: from dsl-216-186.hive.is (HELO ?192.168.1.34?) (85.197.216.186) by mx1.ipf.is with SMTP; 26 May 2006 16:21:17 +0000 Subject: Re: column totals From: Ragnar To: pgsql-performance@postgresql.org In-Reply-To: <4476D0D7.1080006@silversphere.co.za> References: <4476D0D7.1080006@silversphere.co.za> Content-Type: text/plain; charset=ISO-8859-1 Date: Fri, 26 May 2006 16:23:37 +0000 Message-Id: <1148660617.8566.51.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/493 X-Sequence-Number: 19280 On fös, 2006-05-26 at 11:56 +0200, James Neethling wrote: > SELECT branch_id, prod_cat_id, sum(prod_profit) as prod_cat_profit > FROM () as b1 > WHERE x = y > GROUP BY branch, prod_cat_id > > > Now, I also need the branch total, effectively, > SELECT branch_id, sum(prod_profit) as branch_total > FROM () as b1 > WHERE x = y > GROUP BY branch_id. > > > Since the actual queries for generating prod_profit are non-trivial, how > do I combine them to get the following select list? one simple way using temp table and 2 steps: CREATE TEMP TABLE foo AS SELECT branch_id, prod_cat_id, sum(prod_profit) as prod_cat_profit FROM () as b1 WHERE x = y GROUP BY branch, prod_cat_id; SELECT branch_id, prod_cat_id, prod_cat_profit, branch_total FROM foo as foo1 JOIN (SELECT branch_id, sum(prod_cat_profit) as branch_total FROM foo GROUP BY branch_id ) as foo2 USING branch_id; (untested) gnari