X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id D08C4475C15 for ; Wed, 14 May 2003 11:32:25 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 63641-01 for ; Wed, 14 May 2003 11:32:23 -0400 (EDT) Received: from davinci.ethosmedia.com (unknown [209.10.40.251]) by postgresql.org (Postfix) with ESMTP id 3A7984766D7 for ; Wed, 14 May 2003 11:24:51 -0400 (EDT) Received: from [63.195.55.98] (HELO spooky) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.0.2) with ESMTP id 3033251; Wed, 14 May 2003 08:24:57 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Organization: Aglio Database Solutions To: David Siegal Subject: Re: Calculating Percentages Date: Wed, 14 May 2003 08:24:35 -0700 User-Agent: KMail/1.4.3 References: In-Reply-To: Cc: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <200305140824.35328.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/276 X-Sequence-Number: 13441 David, > If I were to do: > SELECT nationality, ((COUNT(*) * 100)/(select count(*) from member)) as > percentage FROM member GROUP BY nationality ORDER BY nationality; > > would this repeatedly execute the inner query over and over? Yes, it would Better is: SELECT nationality, (COUNT(*)*100/total_members) as percentage FROM member, (SELECT COUNT(*) as total_members FROM members) tot_mem GROUP BY nationality ORDER BY nationality This method runs the grand total only once. -- Josh Berkus Aglio Database Solutions San Francisco