题解 | #工作日各时段叫车量、等待接单时间和调度时间#
工作日各时段叫车量、等待接单时间和调度时间
https://www.nowcoder.com/practice/34f88f6d6dc549f6bc732eb2128aa338
with dataList as ( select case when hour(event_time) >= 7 and hour(event_time) <= 8 then '早高峰' when hour(event_time) >= 9 and hour(event_time) < 17 then '工作时间' when hour(event_time) >= 17 and hour(event_time) < 20 then '晚高峰' when hour(event_time) >= 20 or hour(event_time) < 7 then '休息时间' end as workdate, event_time,#这里是一个开始打车时间 end_time, #结束打车时间(也是司机接单时间)减去开始打车时间就是等待时间 start_time#客户上车时间,减去接单时间就是调度时间 from tb_get_car_record join tb_get_car_order using(order_id) where dayofweek(event_time) between 2 and 6 ) SELECT workdate, count(event_time) get_car_num , round(avg(timestampdiff(second,event_time,end_time))/60,1) as avg_wait_time, round(avg(timestampdiff(second,end_time,start_time))/60,1) as avg_dispatch_time #因为需要求出的是多少分钟,所以需要把秒除以60,但是如果使用timestampdiff(minute,day1,day2)返回的数值会取整,不符合要求。还有就是实例中会有start_time为null的情况,也就是说接单后乘客取消订单或者司机取消,就不应计入平均调度时间,在timestampdiff中如果day1,day2参数有一个为null 那么返回值就是null,在avg聚合函数中null是不被统计的。 from dataList group by workdate order by get_car_num
本体难度不大,需要将客户打车记录表和打车订单表通过order_id进连接即可。利用hour()函数求出每天的小时段,要注意区间开关闭合的情况,在时间判断中也可以用else '休息时间',再用dayofweek筛选当天是否是处于工作日,在标准中,1为周日,所以周一到周五是2-6。
至于为什么要在timestampdiff中需要求出second的值,因为需要求出的是多少分钟,所以需要把秒除以60,但是如果使用timestampdiff(minute,day1,day2)返回的数值会取整,不符合要求。还有就是实例中会有start_time为null的情况,也就是说接单后乘客取消订单或者司机取消,就不应计入平均调度时间,在timestampdiff中如果day1,day2参数有一个为null 那么返回值就是null,在avg聚合函数中null是不被统计的。
代码如上所示。