最简单易懂的代码

各城市最大同时等车人数

https://www.nowcoder.com/practice/f301eccab83c42ab8dab80f28a1eef98

最难的就两部分

1、结束等待的时间判定。有上车就是start_time结束等待;没上车,司机接单后取消finish_time;没有司机接单,取消打车end_time

ifnull(start_time,ifnull(finish_time,end_time)

2、单日同时等车人数,先记增加后减少:

sum(cnt)over(partition by city,dt order by tm,cnt desc)

select
    city,max(wait_uv) as max_wait_uv
from (
    select
        distinct city,dt,tm,
        sum(cnt)over(partition by city,dt order by tm,cnt desc) as wait_uv #单日同时等车人数,先记增加后减少
    from (
        select city,date(event_time) as dt,event_time as tm,1 as cnt #开始打车记为1
        from tb_get_car_record
        union all
        select city,date(event_time) as dt,ifnull(start_time,ifnull(finish_time,end_time)) as tm,-1 as cnt #结束等待记为-1。有上车就是start_time结束等待;没上车,司机接单后取消finish_time;没司机接单,取消打车end_time
        from tb_get_car_record left join tb_get_car_order using(order_id)
        ) as t
    where dt like '2021-10%' #2021年10月
) as t1
group by city
order by max_wait_uv,city

全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务