S.K
S.K

Reputation: 530

How to use same psql subquery in from and where clause?

I want to use the same subquery in from and where clause. Tried following two approaches, but getting same error in both cases at different places in the query.

QUERY 1:

select * from (subquery1) as t_feature where id = (select MAX(id) from t_feature);

QUERY 2:

select * from t_feature where id = (select MAX(id) from (subquery1) as t_feature);

ERROR:

ERROR: relation "t_feature" does not exist

For temporary soultion, I have created a view for the subquery, and used that in place of the subquery. But I do not want to create views for this case.

Upvotes: 0

Views: 209

Answers (1)

user330315
user330315

Reputation:

Use a common table expression:

with t_feature as (
   ...
) 
select * 
from t_feature 
where id = (select MAX(id) from t_feature);

Upvotes: 1

Related Questions