题解 | #各城市最大同时等车人数#
各城市最大同时等车人数
https://www.nowcoder.com/practice/f301eccab83c42ab8dab80f28a1eef98
SELECT city, MAX(wait_num) max_wait_uv FROM ( SELECT b.city, date(a.wait_time) dt, # 分组条件:"各个城市","单日" SUM(a.cnt)OVER(PARTITION BY b.city, date(a.wait_time) ORDER BY a.wait_time ASC, a.cnt DESC) wait_num # order by+ 窗口范围(求和的范围) # a.cnt DESC 先增后减“如果同一时刻有人停止等车,有人开始等车,等车人数记作先增加后减少” # order by xx ASC, xx DESC 有逗号!! FROM ( # 三种情况: 1.正常上车 2.司机取消 3.乘客取消 SELECT order_id, event_time AS wait_time, +1 AS cnt FROM tb_get_car_record UNION ALL SELECT order_id, end_time AS wait_time, -1 AS cnt FROM tb_get_car_record WHERE order_id IS NULL # 无订单生成/还没有司机接单 UNION ALL SELECT order_id, IFNULL(start_time, finish_time) AS wait_time, -1 AS cnt FROM tb_get_car_order # 如果没有订单start时间,就是没上车时取消了,取消时间finish_time ) a JOIN tb_get_car_record b ON a.order_id = b.order_id WHERE wait_time LIKE "2021-10%" ) t GROUP BY city, dt ORDER BY max_wait_uv, city