Here is query:
ActiveRecord::StatementInvalid (PGError: ERROR: syntax error at or near "over" select *, rank() over (partition by 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
EDIT: here is a detailed explanation of what I am trying to do: Rails 3 app with PostgreSQL - Getting the list of messages grouped by converstation
It turns out the Heroku shared DB is a PostgreSQL version 8.3 therefore no windows function so the question become how can I do this query in PostgreSql 8.3?
THANKS!
posts.created_at = dt.maxdtcould produce multiple results for a single thread though. You might want to include a client-side check to deal with duplicatethread_id,created_atpairs. The chance of a duplicate is quite low but "low" and "zero" aren't quite the same.