5

鉴于我有一些数据表...我有一个查询,用于检索具有复杂案例子句的数据...

 SELECT row_number() over (...) as num, ... as field 1, 
    case ... as field2 
    FROM ...

现在我想添加更复杂的逻辑,比如

 SELECT row_number() over (...) as num, ... as field 1, 
    field2 * num as field2 
    FROM ...

基本上,如果我输入row_num + 1 as field2Postgres 会返回我

错误:列“row_num”不存在

如何在此查询中引用查询字段?

4

2 回答 2

6

制作一个外部表并在该表中选择字段 2

Select *, num+1 as field2
from
(
   SELECT row_number() over (...) as num, ... as field1 FROM ...
) t
于 2012-06-26T11:08:57.343 回答
0
SELECT row_number() over (...) as num,
       1 + row_number() over (...) as field2,
       ... as field3,
  FROM ...
于 2012-06-26T14:45:51.130 回答