1

我可以row_number() OVER()通过计数行对结果进行编号吗?

例如

SELECT *,
    users::numeric/population::numeric*100 AS penetration,
    row_number() OVER(ORDER BY penetration DESC)
FROM states ORDER BY penetration DESC

产生错误:

ERROR: column "penetration" does not exist
4

2 回答 2

1

你不能在 postgresql 中这样做。在这里寻找expression4.2.8。窗口函数调用,它说..它不能是输出列名称或数字..

您可以用户子查询:

SELECT states.*, penetration, row_number() OVER(ORDER BY penetration DESC)
  FROM states
  JOIN (SELECT id, users::numeric/population::numeric*100 AS penetration
          FROM states) s_pen on spen.id = states.id
 ORDER BY s_pen.penetration DESC
于 2011-04-04T14:26:29.877 回答
1

我不使用 postgre 但对于 t-sql 你必须做这样的事情:

SELECT *,
    penetration,
    row_number() OVER(ORDER BY penetration DESC)
FROM 
(
   select users::numeric/population::numeric*100 AS penetration
   from states
)pen
 ORDER BY penetration DESC
于 2011-04-04T14:19:10.637 回答