我可以通过计算行,通过row_number() OVER()
对结果进行编号吗?
e.g。
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
答案 0 :(得分: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
答案 1 :(得分:1)
你不能在postgresql中这样做。在这里查找expression
:4.2.8. Window Function Calls,它说 ..它不能是输出列名称或数字..
您可以使用子查询:
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