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 456DA5E3F15 for ; Tue, 24 Aug 2004 12:32:44 -0300 (ADT) 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 30618-05 for ; Tue, 24 Aug 2004 15:32:48 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 4CE285E3632 for ; Tue, 24 Aug 2004 12:32:27 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i7OFVnLq030761 for ; Tue, 24 Aug 2004 15:32:09 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i7OFAXrq020772 for pgsql-sql@postgresql.org; Tue, 24 Aug 2004 15:10:33 GMT From: Bruno Mueller X-Newsgroups: comp.databases.postgresql.sql Subject: refer to computed columns Date: Tue, 24 Aug 2004 17:10:11 +0200 Organization: Hub.Org Networking Services Lines: 34 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla Thunderbird 0.7.3 (Windows/20040803) X-Accept-Language: en-us, en To: pgsql-sql@postgresql.org 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: 200408/274 X-Sequence-Number: 18771 Hi Think of the following query: 1 select 2 country, 3 (select sum(salary) from employees) as totalSalary, 4 sum(salary) as countrySalary, 5 countrySalary / totalSalary as countryPct 6 from employees 7 group by country 8 order by country ; I know it does not work, but is there a way to refer to the "computed" columns in line 5, so that I do not have to repeat the calculations again. BTW, are the calculation done more then once if I specify 1 select 2 country, 3 (select sum(salary) from employees) as totalSalary, 4 sum(salary) as countrySalary, 5 sum(salary) / (select sum(salary) from employees) as countryPct 6 from employees 7 group by country 8 order by country ; Thanks for the help Bruno