通过代码

select
    case
    when time(event_time) between '07:00:00' and '08:59:59' then '早高峰'
    when time(event_time) between '09:00:00' and '16:59:59' then '工作时间'
    when time(event_time) between '17:00:00' and '19:59:59' then '晚高峰'
    else '休息时间'
    end period,
    count(*) get_car_num,
    round(sum(timestampdiff(second,event_time,end_time)) / (count(*) * 60),1) avg_wait_time,
    round(sum(timestampdiff(second,order_time,start_time)) / (count(fare) * 60),1) avg_dispatch_time
from
    tb_get_car_record
join
    tb_get_car_order
on
    tb_get_car_record.order_id = tb_get_car_order.order_id
where
    weekday(event_time) between 0 and 4
group by
    period
order by
    get_car_num

思路

统计周一到周五各时段的叫车量、平均等待接单时间和平均调度时间。全部以event_time-开始打车时间为时段划分依据,平均等待接单时间和平均调度时间均保留1位小数,平均调度时间仅计算完成了的订单,结果按叫车量升序排序。

不同时段定义:

早高峰 [07:00:00 , 09:00:00)、

工作时间 [09:00:00 , 17:00:00)、

晚高峰 [17:00:00 , 20:00:00)、

休息时间 [20:00:00 , 07:00:00)

时间区间左闭右开(即7:00:00算作早高峰,而9:00:00不算做早高峰)

从开始打车到司机接单为等待接单时间,从司机接单到上车为调度时间。

题目描述很详细啊

我们照着题目来:

首先是‘周一到周五’:这里我用了 weekday(event_time) between 0 and 4 在weekday函数里周1->7 是0->6而且between函数是两闭区间

所以是0 4

然后 各时段 case when then time函数 也很简单问题上的是左闭右开,这里注意一下

叫车量 : count(*)

平均等待接单时间 : round(sum(timestampdiff(second,event_time,end_time)) / (count(*) * 60),1) avg_wait_time,

平均调度时间: round(sum(timestampdiff(second,order_time,start_time)) / (count(fare) * 60),1) avg_dispatch_time这里计算完成了的订单:所有完成订单得付钱吧。得有公里数吧。评论不一定有

还有就是第二张表内容比第一张表多 所以这里我是用内连接取交集。