我有这样的表
id | name
----------
1 | A
2 | B
5 | C
100 | D
200 | E
201 | F
202 | G
我需要从1 to 3 order by id
排名行,也就是说,我需要结果:
id | name | ranking
---------------------------
1 | A | 1
2 | B | 2
5 | C | 3
100 | D | 1
200 | E | 2
201 | F | 3
202 | G | 1
如何制作?
P.S。
我在尝试:
SELECT id, name, row_number() OVER( order by id RANGE BETWEEN 1 AND 3 ) AS ranking FROM t
这会产生语法错误。
答案 0 :(得分:1)
RANGE
实际上用于其他内容:
http://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS
http://www.postgresql.org/docs/current/static/sql-select.html
尝试使用模数:
SELECT id, name, 1 + (row_number() OVER( order by id ) - 1) % 3 AS ranking
FROM t