Re: Get most recent message between users?

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Get most recent message between users?
Date: 2009-08-18 00:53:40
Message-ID: 20090818005340.GL5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Aug 17, 2009 at 04:33:11PM -0700, Nick wrote:
> messages (id, to_user, from_user, created)
> 1 | 1 | 2 | 2009-05-21
> 2 | 1 | 3 | 2009-06-21
> 3 | 1 | 4 | 2009-07-21
> 4 | 2 | 1 | 2009-05-10
> 5 | 2 | 3 | 2009-06-10
> 6 | 1 | 2 | 2009-07-10
>
> How could I get the most recent message between two users?

More details would help, but failing that how about:

SELECT DISTINCT ON (to_user, from_user) id, to_user, from_user, created
FROM messages
ORDER BY to_user, from_user, created DESC;

That would only chop out id=1 from the above list.

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Greg Stark 2009-08-18 00:59:45 Re: Failover, Wal Logging, and Multiple Spares
Previous Message Yaroslav Tykhiy 2009-08-18 00:25:10 Re: Failover, Wal Logging, and Multiple Spares