PostgreSQL查询在本地工作但不在Heroku上(相同的数据)

时间:2011-09-11 00:31:14

标签: sql ruby-on-rails-3 postgresql heroku

这是查询:

ActiveRecord :: StatementInvalid(PGError:错误:语法错误处于或接近“over”                  select *,rank()over(thread_i分区...                                           ^

     SELECT *
     FROM (                                                                  
             select *, rank() over (partition by thread_id order by created_at DESC)
             from posts
             where circle_id IN (134) OR (receiver_id=3)
           ) as dt
     WHERE rank = 1
编辑:这里是我要做的详细解释: Rails 3 app with PostgreSQL - Getting the list of messages grouped by converstation

事实证明,Heroku共享DB是一个PostgreSQL版本8.3,因此没有Windows函数,所以问题变成如何在PostgreSql 8.3中进行此查询?

谢谢!

2 个答案:

答案 0 :(得分:2)

SELECT p.*
 FROM (                                                                  
         select x.thread_id, max(x.created_at) as maxdt
         from posts x
         where x.circle_id IN (134) OR x.receiver_id=3
         group by x.thread_id
       ) as dt
 INNER JOIN posts p ON p.thread_id = dt.thread_id and p.created_at = dt.maxdt 
 ORDER BY p.created_at DESC

编辑 - 根据评论:

SELECT p.*
 FROM (                                                                  
         select x.thread_id, max(x.created_at) as maxdt, max (OID) maxo
         from posts x
         where x.circle_id IN (134) OR x.receiver_id=3
         group by x.thread_id
       ) as dt
 INNER JOIN posts p ON p.thread_id = dt.thread_id and p.created_at = dt.maxdt AND p.OID = dt.maxo
 ORDER BY p.created_at DESC

答案 1 :(得分:1)

这应该可以帮助你:

 SELECT p.*
 FROM (                                                                  
         select thread_id, max(created_at) as maxdt
         from posts
         where circle_id IN (134) OR (receiver_id=3)
       ) as dt
 INNER JOIN posts p ON p.thread_id = dt.thread_id and p.created_at = dt.maxdt