gpt4 book ai didi

postgresql - 在 OVER() 子句中使用别名

转载 作者:行者123 更新时间:2023-11-29 13:07:12 25 4
gpt4 key购买 nike

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

例如

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

最佳答案

你不能在 postgresql 中这样做。在此处查找 expression:4.2.8. Window Function Calls ,它说 ..it cannot be output-column names or numbers..

您可以使用子查询:

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

关于postgresql - 在 OVER() 子句中使用别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5539695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com