score:0

You can achieve this using Window Functions

SELECT * 
FROM
  (select *, rank() OVER (PARTITION BY date, user_from,user_to ORDER BY time desc ) AS pos
     FROM messages
  ) AS msg

  WHERE pos = 1

I strongly advice you to note use PostgreSQL's tokens as column name!

score:1

Here is how I did it:

SELECT DISTINCT ON (user_from, user_to)   *
FROM messages
ORDER BY user_from, user_to, date+time DESC

More questions

More questions with similar tag