题解 | #工作日各时段叫车量、等待接单时间和调度时间#

工作日各时段叫车量、等待接单时间和调度时间

https://www.nowcoder.com/practice/34f88f6d6dc549f6bc732eb2128aa338

明确题意:统计周一到周五各时间段叫车量,平均等待接单时间和平均调度时间,

平均调度时间仅计算完成了的订单。

这里我用的是date_format对时间进行划分,这里需要注意格式化的时间,和python中datetime的时间格式化符号不同。当然参考其他大佬的答案,使用hour()对时间分段也更加简洁

注意: 涉及到时间加减,最好使用timestampdiff(time,begin,end)。mysql虽然支持直接时间相加减,但是一旦时间位不匹配会出现错误。

还有一个细节,很多聚合函数,对于null列都是会直接忽略的。

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

#数据分析##sql##你的秋招进展怎么样了#
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务