Skip to content
Advertisement

TSQL find number of correspondences

I have the following table:

Test Data:

I need to find out how many times correspondence has been between each pair. For example, Anthony has contacted John 5 times, John has contacted Anthony 5 times. So total correspondence between John and Anthony has been 10 times.

Similarly, David has contacted John total of 9 times, John has contacted David 4 times. So total will be 13. I have tried many things including swapping columns using substring, repalce etc. I was wondering if anyone had interesting way to achieve the wanted results. Thanks in advance.

Advertisement

Answer

You can use a query like the following:

The query uses a CTE that contains a version of the table where SenderName and ReceiverName are selected in an alphabetically sorted order.

Demo here

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