题解 | #各城市最大同时等车人数#
各城市最大同时等车人数
https://www.nowcoder.com/practice/f301eccab83c42ab8dab80f28a1eef98
# 问题:请统计各个城市在2021年10月期间,单日中最大的同时等车人数。
# 重点在等待时间的理解: 三种等待情况:
# 未顺利上车,无司机接单:等带时间为打车开始event_time-end_time ,order_id=null
# 未顺利上车,有司机接单但司机取消:等带时间为打车开始event_time-finish_time,(order_id,finish_time有记录,其余字段设为null)。
# 顺利上车,等待时间为从打车开始event_time-start_time
# 用户开始打车时间容易找,结束时间根据不同情况对应不同字段。创建等车行为的时间表,城市,时间,等车行为(开始等车+1,结束等车-1)
with t as (
# 用户开始打车
select city,event_time as uv_time,1 as uv
from tb_get_car_record
union all
# 无司机接单
select city,end_time as uv_time,-1 as uv
from tb_get_car_record
where order_id is null
union all
# 有司机接单
select city
,ifnull(start_time,finish_time) as uv_time
,-1 as uv
from tb_get_car_record
join tb_get_car_order using(order_id) )
,t1 as(
select *
,sum(uv)over(partition by city order by uv_time,uv desc) as wait_uv
from t
where date_format(uv_time,'%Y-%m')='2021-10' )
select city,max(wait_uv) max_wait_uv
from t1
group by city

查看20道真题和解析