X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 64FD147649A for ; Wed, 14 May 2003 11:31:46 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 62857-04 for ; Wed, 14 May 2003 11:31:43 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id B7FBF4766E5 for ; Wed, 14 May 2003 11:23:36 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h4EFNdU6024227; Wed, 14 May 2003 11:23:39 -0400 (EDT) To: David Siegal Cc: pgsql-sql@postgresql.org Subject: Re: Calculating Percentages In-reply-to: References: Comments: In-reply-to David Siegal message dated "Thu, 08 May 2003 11:02:53 -0400" Date: Wed, 14 May 2003 11:23:39 -0400 Message-ID: <24226.1052925819@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/275 X-Sequence-Number: 13440 David Siegal writes: > 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? No, it'd do it just once, because the sub-select has no dependency on the outer select. (If the sub-select used any variables from the outer level, then it'd have to be done over at each outer row.) You can tell the difference between these cases by looking at EXPLAIN output for the query: do-once sub-selects are labeled InitPlans, do-every-time sub-selects are labeled SubPlans. regards, tom lane