题解 | #工作日各时段叫车量、等待接单时间和调度时间#
工作日各时段叫车量、等待接单时间和调度时间
http://www.nowcoder.com/practice/34f88f6d6dc549f6bc732eb2128aa338
1.先按照题目所说的时间分段,注意统计的是工作日,需要筛选掉非工作日
select event_time,end_time,order_time,start_time,
case when date_format(event_time, '%H-%i-%s') >= '07-00-00' and date_format(event_time, '%H-%i-%s') <'09-00-00'
then '早高峰'
when date_format(event_time, '%H-%i-%s')>='09-00-00' and date_format(event_time, '%H-%i-%s')<'17-00-00'
then '工作时间'
when date_format(event_time, '%H-%i-%s')>='17-00-00' and date_format(event_time, '%H-%i-%s')<'20-00-00'
then '晚高峰'
else'休息时间' end period
from
tb_get_car_record
join
tb_get_car_order
using(order_id)
where date_format(event_time,'%W') not in ('Saturday','Sunday')
得到下表
2.分时间段计算叫车量,平均等待接单时间和平均调度时间
叫车量:count(order_time)
平均等待接单时间(接单时间-打车时间):avg(timestampdiff(second,event_time,order_time)/60)
平均调度时间(上车时间-接单时间):avg(timestampdiff(second,order_time,start_time)/60)
#注意AVG() 求平均值 忽略NULL值,所以不统计有为null的数据
3.最终的sql
with t1 as
(select event_time,end_time,order_time,start_time,
case when date_format(event_time, '%H-%i-%s') >= '07-00-00' and date_format(event_time, '%H-%i-%s') <'09-00-00'
then '早高峰'
when date_format(event_time, '%H-%i-%s')>='09-00-00' and date_format(event_time, '%H-%i-%s')<'17-00-00'
then '工作时间'
when date_format(event_time, '%H-%i-%s')>='17-00-00' and date_format(event_time, '%H-%i-%s')<'20-00-00'
then '晚高峰'
else'休息时间' end period
from
tb_get_car_record
join
tb_get_car_order
using(order_id)
where date_format(event_time,'%W') not in ('Saturday','Sunday'))
select period,
count(order_time) get_car_num,
round(avg(timestampdiff(second,event_time,order_time)/60),1) avg_wait_time,
round(avg(timestampdiff(second,order_time,start_time)/60),1) avg_dispatch_time
from t1
group by period
order by get_car_num;