select city,
max(sum_wait) max_wait_uv
from (select *,
sum(tag) over(partition by city order by tm,tag desc) sum_wait
from (select *
from (select city,
a.uid,
event_time tm,
1 as tag
from tb_get_car_order a
left join tb_get_car_record b
on a.order_id = b.order_id
union
select city,
a.uid,
start_time tm,
-1 as tag
from tb_get_car_order a
left join tb_get_car_record b
on a.order_id = b.order_id
) t
where date_format(tm,'%Y-%m') = '2021-10'
) t
) t
group by city
order by max_wait_uv,city