I have a table with sample data;table
email_id | sender_id | received_id |email_cnt
1 | 101 | 102 | t1
2 | 101 | 103 | t2
3 | 102 | 103 | t3
4 | 103 | 104 | t4
Here email_id is unique identifier. The goal is to find email cnt per user. As we can see, total email_cnt is t1+t2+t3+t4. And we have 4 unique users. Hence in the end, we should be able to get (t1+t2+t3+t4)/4 as the answer.
My idea is to first identify caller who are also receivers.
SELECT tab1.receiver_id,
tab1.email_cnt AS received,
tab2.sender_id,
tab2.email_cnt AS made
FROM table tab1
JOIN table tab2
ON tab1.receiver_id=tab2.caller_id;
This gives following output;
receiver_id | received |sender_id | made
102 | t1 | 102 | t3
103 | t2 | 103 | t4
103 | t3 | 103 | t4
The problem is the value t4 is getting repeated due to the join here. It might lead to over counting the email_cnt later on. Can I kindly get some lead on how to tackle this problem?thanks