PostgreSQL 中的行编号
当结果按某列排序时,如何在 PostgreSQL 中获取行号?
例如,
SELECT 30+row_number() AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
我认为查询将返回这样的列表:
position | name | salary
31 | Joy | 4500
32 | Katie| 4000
33 | Frank| 3500
实际上,我必须将 ORDER
子句复制到查询中以使其起作用:
SELECT 30+row_number(ORDER BY salary DESC) AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
是否有其他方法可以返回有序和编号的结果,而无需复制代码?
我知道这可以通过增加应用程序本身中的一些变量来解决,但我想在数据库层执行此操作并返回到应用程序已经编号的结果...
How to get row number in PostgreSQL when the results are ordered by some column?
e.g.
SELECT 30+row_number() AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
I supposed that the query would return list like this:
position | name | salary
31 | Joy | 4500
32 | Katie| 4000
33 | Frank| 3500
Actually i have to duplicate the ORDER
clause into the query to make it functional:
SELECT 30+row_number(ORDER BY salary DESC) AS position, *
FROM users
ORDER BY salary DESC
LIMIT 30
OFFSET 30
Is there any other way how to return ordered and numbered results without necessity of duplicating the code?
I know this can be solved by incrementing some variable in the app itself, but i wanna do this at the database layer and return to the app already numbered results...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否 - 窗口函数中的
order by
和select
语句中的order by
子句在功能上是两个不同的东西。另外,您的语句会生成:
错误:窗口函数调用需要 OVER 子句
,因此:应该是:
请注意,如果工资不唯一,则无法保证它们甚至会产生相同的订单。也许这样做会更好:
另请注意,如果您使用不同的偏移量多次运行此查询,则需要:
,否则可能会出现重复和丢失的行。请参阅 这个答案说明原因。
no - the
order by
in the windowing function and theorder by
clause of theselect
statement are functionally two different things.Also, your statement produces:
ERROR: window function call requires an OVER clause
, so:should be:
Note that if salaries are not unique then there is no guarantee that they will even produce the same order. Perhaps it would be better to do:
Also note that if you are running this query several times with different offsets, you need to:
or you may get duplicates and missing rows. See the comments on this answer for why.