0

I am looking for help converting my MySQL query to work in PostgreSQL. This is a query on the model of RoR application. Any help is appreciated.

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)
leonbloy
  • 73,180
  • 20
  • 142
  • 190
VladofCM
  • 15
  • 3

1 Answers1

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 = ...

For more details on window functions (the over (...) clause) please see the manual:
http://www.postgresql.org/docs/current/static/tutorial-window.html