我正在寻找帮助将我的MySQL查询转换为在PostgreSQL中工作的帮助。这是对RoR应用程序模型的查询。 任何帮助表示赞赏。
SELECT * FROM(
SELECT @row := @row + 1 AS rownum, id, device_id, name, quarterly
FROM (SELECT @row :=0) r, recurrent_tests
WHERE device_id = "+self.id.to_s+" AND quarterly = 1
ORDER BY name ASC
) ranked
WHERE (rownum-1) % 4 = "+(i-1).to_s)
答案 0 :(得分:2)
select *
from (
select row_number() over (order by name asc) as rownum,
id,
device_id,
name,
quarterly
from recurrent_tests
) t
where rownum - 1 % 4 = ...
有关窗口功能的更多详细信息(over (...)
子句),请参阅手册:
http://www.postgresql.org/docs/current/static/tutorial-window.html