如何在 from 和 where 子句中使用相同的 psql 子查询?

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

我想在 from 和 where 子句中使用相同的子查询。 尝试了两种方法,但在查询的不同位置两种情况下都出现了相同的错误。

查询 1:

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

查询 2:

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

错误:

ERROR: relation "t_feature" does not exist

为了临时解决问题,我为子查询创建了一个视图,并用它来代替子查询。但是我不想为这种情况创建视图。

使用 common table expression:

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