这是查询:
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中进行此查询?
谢谢!
答案 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