The call log has the following columns:
CallingParty, CalledParty, Duration, EventTime
and the example data is the following:
X, a, 10, 10:20
X, b, 12, 10:34
X, c, 8, 12:08
a, X, 22, 12:45
X, a, 10, 13:55
d, X, 30, 15:01
What I would like to do is to calculate the statistics for each contact (how many times did the user X call the contact, what was the sum of outgoing calls duration, how many times did the contact call user X, and what was the sum of incoming calls duration). Actually I am trying to mine the data from the call log of user X.
The statistics for the example above would be the following:
contactName, incomingCallsCount, IncomingCallsDuration, OutgoingCallsCount, OutgoingCallsDuration
a, 1, 10, 2, 20
b, 0, 0, 1, 12
c, 0, 0, 1, 8
d, 1, 30, 0, 0
I tryed the following query with different joins () but couldn’t fet the right result
SELECT t1.CallingParty AS Contact, t1.CallingPartyCount, t1.CallingPartyDuration, t2.CalledPartyCount, t2.CalledPartyDuration FROM
(SELECT e.CallingParty, COUNT(*) AS CallingPartyCount, SUM(CAST(REPLACE(e.Duration, 'NULL', '0') AS int)) AS CallingPartyDuration FROM Events e WHERE Duration <> 'NULL' GROUP BY e.CallingParty) t1
<JOIN>
(SELECT e.CalledParty, COUNT(*) AS CalledPartyCount, SUM(CAST(REPLACE(e.Duration, 'NULL', '0') AS int)) AS CalledPartyDuration FROM Events e WHERE Duration <> 'NULL' GROUP BY e.calledParty) t2
ON t1.CallingParty = t2.CalledParty
Does anyone know what would be the right query in order to get the correct statistics?
Thank You!
You shouldn’t be joining to begin with, it sounds more like a simple group by with sum and count.
Now, you also need a set of all the users involved (a,b,c and d), that can be computed by using a UNION if you don’t have this somewhere else.
Then you just put these together using CTEs.
That’s should do it!