Skip to content
Advertisement

Select the latest message thread values from a table using sql

This is my table

How do I select a row with out duplication based on [ReceiverId, SenderId] pair and Ordered by Id in Descending order. That is: [5, 1]=[1,5] are duplicate. OR [5,1] = [5,1] are also the duplicate.

So the final result should be:

Advertisement

Answer

Assuming that among records, which you consider to be the same by just checking the SenderId and ReceiverId (order doesn’t matter), you want the one with the largest Id (which could probably be the latest). Then, this query will give you the result:

Replace MyTable with your table’s name.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement