题解 | #各城市最大同时等车人数#

各城市最大同时等车人数

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

with tmp as (SELECT city,event_time uv_time,1 AS uv FROM  tb_get_car_record 
UNION ALL
SELECT city,end_time uv_time,-1 AS uv FROM  tb_get_car_record WHERE order_id IS NULL #接单前取消
UNION ALL
SELECT city,IFNULL(start_time,finish_time) uv_time,-1 AS uv FROM tb_get_car_order LEFT JOIN tb_get_car_record USING(order_id))

select city,max(uvcnt) as max_wait_uv from (select city,
    sum(uv) over(partition by city order by uv_time,uv desc) as uvcnt
from tmp where left(uv_time,7) = '2021-10') t 
group by city order by max(uvcnt),city;

首先明确等车时间的定义:

  • 如果订单是正常状态,那么event_time uv=1;
  • 如果司机接单前取消订单即order_id 为null,则用end_time表示 uv=-1;
  • 如果司机接单后取消,那么start_time可能为null ,则用IFNULL(start_time,finish_time)表示 uv=-1

瞬时问题 :

  • 根据city分区 按照dt正序 uv降序进行sum求和 展示每天的最大同时在线人数
  • 根据city分组 求uvcnt最大时的city和max(uvcnt
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 17:10
点赞 评论 收藏
分享
06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务