I have the following table
create table messages(
id bigserial primary key,
created_at timestamp with time zone,
message text,
from_id bigint,
to_id bigint
);
I'm select all unique conversations by using the following query
SELECT DISTINCT ON (LEAST(from_id, to_id), GREATEST(from_id, to_id))
id, message, created_at
FROM messages
ORDER BY LEAST(from_id, to_id), GREATEST(from_id, to_id), created_at DESC;
But I'd like to change it so I get the last message from each conversation. Not the first. Any idea how I can do that? Possible using a different query?