Re: function expression in FROM may not refer to other relations - Mailing list pgsql-sql

From Joe Conway
Subject Re: function expression in FROM may not refer to other relations
Date
Msg-id 411908DA.6090305@joeconway.com
Whole thread Raw
In response to function expression in FROM may not refer to other relations of same query level  ("Philippe Lang" <philippe.lang@attiksystem.ch>)
List pgsql-sql
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


pgsql-sql by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: sleep function
Next
From: Theo Galanakis
Date:
Subject: ERROR: Cross-database references are not implemented