题解 | #各城市最大同时等车人数#
#这个题按照道理来讲不能算难题,重点在于读懂题(这里感觉出题人就喜欢搞一下模糊概念,不讲清楚,故意让你去调。实际开发敢这么给,估计会被砍),自己推的逻辑如下(有可能有其他理解,这里我也没有看其他的解法):2023-6-21的改进版
select city,max(wait_uv) as max_wait_uv from ( select city,sum(flag) over(partition by city order by op_time,flag desc) as wait_uv from ( select city,event_time as op_time, 1 as flag from tb_get_car_record where date_format(event_time,'%Y-%m')='2021-10' union all select city,end_time as op_time, -1 as flag from tb_get_car_record where order_id is null and end_time is not null and date_format(end_time,'%Y-%m')='2021-10' union all select city,if(start_time is not null,start_time,finish_time) as op_time, -1 flag from tb_get_car_order a join tb_get_car_record b on a.order_id=b.order_id and ((start_time is not null and date_format(start_time,'%Y-%m')='2021-10' ) or (finish_time is not null and date_format(finish_time,'%Y-%m')='2021-10')) ) t1 ) t2 group by city order by max_wait_uv,city