坑点

调度时间的计算比接单时间多了一个订单已完成的条件

解决办法

利用未完成订单没有start_time这个特点

  • 在计算时间差的时候使用ifnull(start_time,order_time),使得该订单的接单时间为0
  • 在计算总订单数的时候使用count(start_time)过滤掉未完成的订单
  • 两者相除即可得平均的调度时间

weekend()函数

weekend(d)

日期 d 是星期几,0 表示星期一,1 表示星期二

完整语句

select period,
       count(period) get_car_num,
       round(avg(wait_time)/60,1) avg_wait_time,
       round(sum(dispatch_time)/count(start_time)/60,1) avg_dispacth
from(SELECT CASE
            WHEN TIME(event_time) < '07:00:00' THEN '休息时间'
            WHEN TIME(event_time) < '09:00:00' THEN '早高峰'
            WHEN TIME(event_time) < '17:00:00' THEN '工作时间'
            WHEN TIME(event_time) < '20:00:00' THEN '晚高峰'
            ELSE '休息时间'
            END period,
            weekday(event_time) weekdays,
            start_time,
        TIMESTAMPDIFF(SECOND , event_time, end_time)   wait_time,
        TIMESTAMPDIFF(SECOND, order_time, ifnull(start_time,order_time)) dispatch_time
    FROM tb_get_car_record
    JOIN tb_get_car_order USING (order_id)
) table_1
where weekdays in (0,1,2,3,4)
group by period
ORDER BY get_car_num