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 5BB3A14B2866 for ; Tue, 10 Aug 2004 14:42:02 -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 25201-04 for ; Tue, 10 Aug 2004 17:41:57 +0000 (GMT) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 3670F14B2820 for ; Tue, 10 Aug 2004 14:41:55 -0300 (ADT) Received: from [206.19.64.3] (account jconway HELO [192.168.57.23]) by joeconway.com (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 2099641; Tue, 10 Aug 2004 10:34:25 -0700 Message-ID: <411908DA.6090305@joeconway.com> Date: Tue, 10 Aug 2004 10:41:46 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Philippe Lang Cc: pgsql-sql@postgresql.org Subject: Re: function expression in FROM may not refer to other relations References: <6C0CF58A187DA5479245E0830AF84F420803B2@poweredge.attiksystem.ch> In-Reply-To: <6C0CF58A187DA5479245E0830AF84F420803B2@poweredge.attiksystem.ch> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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/74 X-Sequence-Number: 18571 Philippe Lang wrote: > But the same query with a parameter returns an error: > > select id, usr, code, line1, line2 from tbl, get_lines(code); > --> ERROR: function expression in FROM may not refer to other relations > of same query level This is as expected and required -- you cannot refer to other FROM clause relations. I believe SQL99 defines a clause (LATERAL IIRC) that would allow this, but it hasn't been implemented yet. > Is there another way to run this query and get: > > id usr code line1 line2 > ---------------------------------- > 1 one 1 A B > 2 two 2 Z Z > 3 three 1 A B Whats wrong with just using CASE: select id, usr, code, case when code = 1 then 'A' else 'Z' end as line1, case when code = 1 then 'A' else 'Z' end as line2 from tbl; id | usr | code | line1 | line2 ----+-------+------+-------+------- 1 | one | 1 | A | A 2 | two | 2 | Z | Z 3 | three | 1 | A | A (3 rows) Joe