MYSQL - Average Time to Reply to Message

Is it possible to calculate the average time to reply to a message just with the following columns:

id | ref | client | admin | date | message
  • id is the unique message number
  • ref is the message reference number, which is not unique (searching for ref, ordering by date will show a conversation)
  • client is ID of client, if it is a client message, else 0 if not a client
  • admin is ID of admin, if it is an admin message, else 0 if not a client
  • date is set up using datetime being the time of the message
  • message being 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

This question and answers originated from www.stackoverflow.com
Question by (11/8/2011 10:00:03 PM)

Answer

Your question is well formulated but leaves room for interpretation. This is one interpretation:

SELECT avg(TIMESTAMPDIFF(SECOND, c.c_date, a.a_date) AS avg_time_to_response
FROM   (
    SELECT ref, min(date) AS c_date
    FROM   tbl
    WHERE  client > 0
    GROUP  BY 1
    ) c
JOIN  (
    SELECT ref, min(date) AS a_date
    FROM   tbl
    WHERE  admin > 0
    GROUP  BY 1
    ) a USING (ref)
WHERE a.a_date > c.c_date;

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 1

I quote the manual here:

Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1:

Emphasis mine. So I group on the first column that is selected (ref in both cases). Just a notational shortcut.


Find More Answers
Related Topics  mysql  database  query  time  average
Related Questions
  • Mysql Average on time column?

    SELECT avg( duration ) as average FROM `login`; The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc I execute the query it gives me: 2725.78947368421 What is…
  • What's the most efficient way to get the horizontal average in a MySQL query?

    I have the following MySQL-table Id | One | Two | Three ---------------------------- 1 | 10 | 30 | 20 2 | 50 | 60 | 20 3 | 60 | NULL | 40 Edit: Of course the table doesn't need to be …
  • SQL Work out the average time difference between total rows

    I've searched around SO and can't seem to find a question with an answer that works fine for me. I have a table with almost 2 million rows in, and each row has a MySQL Date formatted field. I'd l…
  • Java Time Specific Average

    I have a text file: DATE 20090105 1 2.25 1.5 3 3.6 0.099 4 3.6 0.150 6 3.6 0.099 8 3.65 0.0499 DATE 20090105 DATE 20090106 1 2.4 1.40 2 3.0 0.5 5 3.3 0.19 7 2.75 0.5 10 2.75 0.25 DATE 20090106 DA…
  • SQL query to obtain an Average value for set periods of time

    I have a MySQL database which is used to store power readings, with a reading added once per minute. (i.e. 1,440 readings per day). time power ---- ----- 00:00:00 346 00:01:00 352 00:02:00 …
  • SQL Query to get Average time

    I have 4 columns in my database UserName, Date, Time, LoginType UserName is a varchar field Date is a date field Time is a time field (in 24 hour format) LoginType is a varchar field Login…
  • Fastest way to perform time average of multiple calculations in SQL?

    I have a question about the fastest way to perform a SQL Server query on a table, TheTable, that has the following fields: TimeStamp, Col1, Col2, Col3, Col4 I don't maintain the database, I just ca…
  • Query to calculate average time between successive events

    My question is about how to write an SQL query to calculate the average time between successive events. I have a small table: event Name | Time stage 1 | 10:01 stage 2 | 10:03 stage…
  • phpmyadmin average time

    I am working with a joomla helpdesk component (Huruhelpdesk) and i need to work a query to see the average time of the tickets since the user open the ticket (start_date) till the reps close the tic…
  • How do I difference time in MySQL query?

    There's a datetime field "time" and it's in Y-m-d H:i:s format. All the values are past values. I need to query that field diffed from current time resulting in seconds. SELECT time FROM table