On Mon, 27 Aug 2001, Joseph Shraibman wrote:

> OK my query was like:
> select u.field1, ... , d.field1, ... (select ml.field from ml WHERE ml.key = u.key 
>order 
> by ml.keyfield desc limit 1) as lastml FROM utable u, dtable d, WHERE u.key = d.key 
>and 
> ... and (sublectect again) = 2;
> 
> OK when I tried to convert the query:
> select u.field1, ... , d.field1, ..., mll.lastml FROM utable u, dtable d,(select 
>ml.field 
> as lastml from ml WHERE ml.key = u.key order by ml.keyfield desc limit 1) mll WHERE 
>u.key 
> = d.key and ... and lastml = 2;
> 
> I got:
> ERROR:  Relation 'u' does not exist
> 
> 
> The key fields in the subselect must match up with the key fields in the outside 
>query. 
> I'm afraid this would indicate that they would not match up (even if I didn't use 
>short 
> aliases for the tables) and the result of the subselect would be meaningless.

I think you'd want to move the entire query excepting the lastml where
condition into a single subselect in the outer from with the lastml
condition on the outside:
 select * from (<old query minus lastml condition>) as blah where
  lastml=2;

However, I don't think this changes the computation that it's doing
(simple example explains still show two subquery runs).


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Reply via email to