A_J A_J - 5 months ago 9
SQL Question

SQLite3, I need groupby on two columns where the combination of them should be unique

I have a table chats, with columns sender, receiver....
For example I have below data:


sender receiver

id1 id2

id1 id3

id2 id1

id3 id2

id2 id3

id3 id2


I want result as


sender receiver

id1 id2

id1 id3

id3 id2


Please suggest how I could use GROUP BY sender, receiver.
Thanks

vkp vkp
Answer

Use < or > so only one combination of sender and receiver gets selected.

select * from chats 
where sender < receiver --or sender > receiver
order by sender,receiver 
Comments