如何计算考虑营业时间的平均响应时间?

时间:2017-08-02 13:50:43

标签: php mysql

我试图计算医生的平均响应时间。

这是我的数据库架构:

mysql> select * from consultation limit 2;
+----+------------------------+
| id | consultation_status_id |
+----+------------------------+
|  1 |                      1 |
|  2 |                      1 |
+----+------------------------+
2 rows in set (0.00 sec)


mysql> select * from consultation_message limit 2;
+----+-----------------+-----------+---------------------------+---------------------------+
| id | consultation_id | author_id | consultation_message_text | created_at                |
+----+-----------------+-----------+---------------------------+---------------------------+
|  1 |               1 |         1 | How to treat a fever?     | 2017-07-28 16:01:48       |
|  2 |               1 |         2 | Take Paracetamol          | 2017-07-31 09:03:50       |
+----+-----------------+-----------+---------------------------+---------------------------+
2 rows in set (0.00 sec)  

我只需考虑营业时间(周一至周五09:00 - 17:00)。鉴于上述数据,结果应为:

mysql> select id, average_reply_time_in_hours * from user where author_id = 2;
+-----------+-----------------------------+
| author_id | average_reply_time_in_hours |
+-----------+-----------------------------+
|         2 |                           1 |
+-----------+-----------------------------+
1 row in set (0.00 sec)    

因为问题是在下午4点之前提交的,工作日在一小时后结束,而且回复是在上午9点提交的,所以计算中包含的唯一一小时是星期五晚上的一小时。

1 个答案:

答案 0 :(得分:0)

这是一个开始。这告诉您两个日期之间有多少个工作日:

SELECT 5 * (DATEDIFF(LEAST(a.end,LAST_DAY(a.start)),a.start) DIV 7) + MID('0123444401233334012222340111123400001234000123440', 7 * WEEKDAY(a.start) + WEEKDAY(a.end) + 1, 1) total 
  FROM 
 (SELECT x.created_at start
     , MIN(y.created_at) end
  FROM consultation_message x
  JOIN consultation_message y
    ON y.consultation_id = x.consultation_id
   AND y.id > x.id
 GROUP 
    BY x.created_at
    ) a;