I am working on an API-based simple chat module. I am trying to get chat conversations for a particular user but due to 2 columns having the same value swapped between each other is causing my data to be duplicated.
I want to merge rows having the same values swapped between 2 columns and the merged row should be based on the latest entry inserted in the database.
The data looks like this :
Id To From Message ConversationTime
1 1 2 hello 11:00AM
2 3 1 hi 12:00PM
3 1 3 how are you? 12:15PM
4 3 1 I am fine. 12:30PM
5 4 5 Hi! 04:30PM
6 5 4 Hello 04:35PM
7 1 5 Hola! 06:30PM
So for example if user with Id 1 My result needs to look like this:
Id To From Message ConversationTime
1 1 2 hello 11:00AM
4 3 1 I am fine. 12:30PM
7 1 5 Hola! 06:30PM
If Id is 5 then result would be like this:
Id To From Message ConversationTime
6 5 4 Hello 04:35PM
7 1 5 Hola! 06:30PM
My result set looks like this:
Id To From Message ConversationTime
1 1 2 hello 11:00AM
3 1 3 how are you? 12:15PM
4 3 1 I am fine. 12:30PM
7 1 5 Hola! 06:30PM
Any help would be grateful. Thanks in advance!