I have the following data:
Now what I am trying to do is get all unique rows (sorted by created_at) where the source_account_id and target_account_id is unique and latest row for source or target == 1
I have tried this but it returns 4 rows when it should return basically only 2:
select
*
from
messages
where
source_account_id = 1 OR target_account_id = 1
group by
source_account_id,
target_account_id
The result I expect is 2 rows with message_id = 3, 6.
To sum it up really I want the latest row (message) for account_id = 1 and any people he has sent or received a message from
Any ideas?