题解 | #各城市最大同时等车人数#
各城市最大同时等车人数
http://www.nowcoder.com/practice/f301eccab83c42ab8dab80f28a1eef98
借鉴了之前一些大佬如何求同时最大数的解法。 步骤一:确定司机未接单前,乘客取消订单、司机接单后,乘客或司机取消订单、订单正常,乘客上车时间三种状态下的等待时间。(第一个case...when) 步骤二:确定乘客当前状态。即上车前,等待人数加1(+1),上车后等待人数减1(-1)。新建一个临时字段放置标签。窗口函数中需要将这个字段进行倒序。确保+1在前,符合题意说的,先加再减。 步骤三:求最大值。我尝试将round函数在13行和14行分别用了一次,0和1也写了一次。发现最后结果仍有3个0。但在本地运行时结果是正常的,所以偷鸡使用Left只取1个数。
with a as (
select city,event_time,
case when t1.order_id is null
then end_time
when t1.order_id is not null and mileage is null
then finish_time
else start_time
end jieshu
from tb_get_car_record t1
left join tb_get_car_order t2
on t1.order_id=t2.order_id)
select city,left(max(cnt),1) max_wait_uv from (
select city,sum(num)over(partition by city order by event_time,num desc) cnt from (
select city,event_time,'1' as num from a
union all
select city,jieshu,'-1' as num from a ) b
where event_time like '2021-10%') c
group by city
order by max_wait_uv,city