题解 | #工作日各时段叫车量、等待接单时间和调度时间#
工作日各时段叫车量、等待接单时间和调度时间
https://www.nowcoder.com/practice/34f88f6d6dc549f6bc732eb2128aa338
select period ,count(order_id) as get_cat_num ,round(avg(wait_time),1) as avg_wait_time ,round(sum(dispatch_time)/sum(if_finish),1) as avg_dispatch_time from ( select date(event_time) ,substr(event_time,12,2) ,event_time,order_time,start_time ,re.order_id as order_id ,case when substr(event_time,12,2) >=7 and substr(event_time,12,2) <9 then '早高峰' when substr(event_time,12,2) >=9 and substr(event_time,12,2)<17 then '工作时间' when substr(event_time,12,2) >=17 and substr(event_time,12,2)<20 then '晚高峰' else '休息时间' end as period ,timestampdiff(second,event_time,order_time)/60 as wait_time # ,timestampdiff(minute,event_time,order_time) as wait_time1 #时间参数只保留整数位,故需要秒进行转换 ,timestampdiff(second,order_time,start_time)/60 as dispatch_time ,if(mileage>0,1,0) as if_finish from tb_get_car_record re left join tb_get_car_order ord on re.order_id=ord.order_id where weekday(event_time) between 0 and 4 ) a group by period order by get_cat_num
解题思路:
1.先构建中间表,进行表关联后进行数据过滤,用where过滤为周一到周五,用weekday()函数;
2.统计中间表字段,叫车单号/时段/时段分类/每单的等待时间差/每单的调度时间差,注意到秒到维度后,在处理为以分钟为单位,时间差函数timestampdiff(second,,)/60 ;
3.添加是否完成订单字段if_finish,用于计算平均调度时间
4.以时段分组period,聚合统计时段叫车量/平均等待时间/平均调度时间
5.输出结果小数位数处理round(),并按叫车量升序排序
知识点补充:
1.日期进行时间切割,取出时段
2.日期进行星期的处理判断.WEEKDAY函数接受1个参数,即DATE或DATETIME值,它返回一个整数,范围从0到6,表示星期一到星期日。
3.时间差计算。timestampdiff()函数的熟练使用
timestampdiff(minute,event_time,order_time) as wait_time1
#时间参数只保留整数位,故需要秒进行转换