Is it possible to calculate the average time to reply to a message just with the following columns:
id | ref | client | admin | date | message
idis the unique message numberrefis the message reference number, which is not unique (searching for ref, ordering by date will show a conversation)clientis ID of client, if it is a client message, else 0 if not a clientadminis ID of admin, if it is an admin message, else 0 if not a clientdateis set up usingdatetimebeing the time of the messagemessagebeing the message sent
Example Data:
1 | 1 | 1 | 0 | 2011-11-07 01:00:00 | ABC
2 | 1 | 1 | 0 | 2011-11-07 01:01:00 | DEF
3 | 1 | 0 | 1 | 2011-11-07 01:05:00 | abc
4 | 2 | 2 | 0 | 2011-11-07 01:10:00 | 123
5 | 3 | 1 | 0 | 2011-11-07 01:11:00 | abc
6 | 2 | 0 | 1 | 2011-11-07 01:20:00 | a
7 | 3 | 0 | 2 | 2011-11-07 02:11:00 | b
Ideally looking for the average time period between a client message and an admin message, though if there are 2 client messages from the same client (ie. admin didn’t reply to first message before client added their 2nd message) with the same ref.
From example, time for (1) = 5 minutes, (2) = 10 minutes, (3) = 60 minutes … average = 25 minutes (1500 seconds – happy to get work with seconds)
I’m not sure how to even begin working on this…. I do hope someone can help :S
Your question is well formulated but leaves room for interpretation. This is one interpretation:
Gives you the average time that passes between the first client posting and the first admin posting per thread (message reference number).
Unanswered messages are ignored. Threads started by Admins would confuse the result with negative durations, so I excluded those. Only the first response time per thread goes into the result. Additional postings on the same thread are ignored here.
Read the manual here about
TIMESTAMPDIFF().Thanx to @MrJ and @Vincent for pointing out the mistake with the subtraction of timestamps!
Concerning
GROUP BY 1I quote the manual here:
Emphasis mine. So I group on the first column that is selected (
refin both cases). Just a notational shortcut.