Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count highest no of conversation between two clients

Tags:

sql

mysql

db2

I have a table called email with three columns say id, emailFrom, emailTo

--------------------------------------------------
| id | emailFrom        | EmailT0                |
--------------------------------------------------
| 1  | A                | B                      |
--------------------------------------------------
| 2  | B                | A                      |
--------------------------------------------------
| 3  | A                | B                      |
--------------------------------------------------
| 4  | C                | A                      |
--------------------------------------------------
| 5  | B                | C                      |
--------------------------------------------------
| 6  | A                | C                      |
--------------------------------------------------
| 7  | A                | B                      |
--------------------------------------------------

now my question is , how to find out the highest conversation between two clients

i.e. A send mail to B and B send mail to A, it means they have conversation of count 2. now,
I want to find that which users have send most emails to each other

like image 676
Hitesh Kumar Avatar asked Jun 28 '13 12:06

Hitesh Kumar


1 Answers

This will return the users that have sent most emails to each other:

SELECT
  LEAST(emailFrom, emailTo) email1,
  GREATEST(emailFrom, emailTo) email2,
  COUNT(*)
FROM
  yourtable
GROUP BY
  LEAST(emailFrom, emailTo),
  GREATEST(emailFrom, emailTo)
ORDER BY
  COUNT(*) DESC
LIMIT 1

Please see fiddle here.

like image 108
fthiella Avatar answered Oct 24 '22 08:10

fthiella