问题:统计周一到周五各时段的叫车量、平均等待接单时间和平均调度时间。
全部以event_time-开始打车时间为时段划分依据,平均等待接单时间和平均调度时间均保留1位小数,平均调度时间仅计算完成了的订单,结果按叫车量升序排序。
注:
不同时段定义:早高峰 [07:00:00 , 09:00:00)、工作时间 [09:00:00 , 17:00:00)、晚高峰 [17:00:00 , 20:00:00)、休息时间 [20:00:00 , 07:00:00)
时间区间左闭右开(即7:00:00算作早高峰,而9:00:00不算做早高峰)
从开始打车到司机接单为等待接单时间,从司机接单到上车为调度时间。
表1: 用户打车信息表:t_taxi_info
id(主键id), uuid(用户id), order_id(订单id), city_name(城市), event_time(开始打车时间), end_time(结束打车时间:接单/订单取消)
表2: 订单信息表:t_order_info
id(主键id), uuid(用户id), order_id(订单id), driver_id(司机id), order_time(接单时间), start_time(订单开始时间), finish_time(订单完成时间), fare(费用), grade(评分)
注: 两张表的order_id 都是唯一值
-- 数据准备
WITH t_taxi_info AS (
SELECT * FROM (
VALUES (1, 1001, 90004, '深圳市', '2023-12-04 11:00:12', '2023-12-04 11:00:42')
, (2, 1002, 90011, '深圳市', '2023-12-05 21:00:22', '2023-12-05 21:01:06')
, (3, 1002, 90008, '深圳市', '2023-12-05 19:01:02', '2023-12-05 19:01:52')
, (4, 1003, 90006, '深圳市', '2023-12-06 09:01:06', '2023-12-06 09:01:36')
, (5, 1004, 90012, '深圳市', '2023-12-06 18:01:08', '2023-12-06 18:01:48')
, (6, 1004, 90018, '深圳市', '2023-12-07 08:01:16', '2023-12-07 08:01:43')
, (7, 1006, 90023, '深圳市', '2023-12-07 08:01:23', '2023-12-07 08:02:01')
, (8, 1007, 90002, '深圳市', '2023-12-08 20:01:12', '2023-12-08 20:01:39')
, (9, 1006, 90017, '深圳市', '2023-12-08 08:30:18', '2023-12-08 08:30:36')
) AS table_name(id, uuid, order_id, city_name, event_time, end_time)
)
, t_order_info AS (
SELECT * FROM (
VALUES (1, 1001, 90004, 2002, '2023-12-04 11:00:42', '2023-12-04 11:05:22', '2023-12-04 11:40:41', 38, 4.8)
, (2, 1002, 90011, 2004, '2023-12-05 21:01:06', '2023-12-05 21:04:02', '2023-12-05 21:52:52', 46, 4.6)
, (3, 1002, 90008, 2005, '2023-12-05 19:01:52', '2023-12-05 19:08:52', '2023-12-05 19:33:20', 29, 4.3)
, (4, 1003, 90006, 2004, '2023-12-06 09:01:36', '2023-12-06 09:03:36', '2023-12-06 09:36:30', 30, 4.5)
, (5, 1004, 90012, 2008, '2023-12-06 18:01:48', '2023-12-06 18:02:48', '2023-12-06 18:42:20', 46, 4.7)
, (6, 1004, 90018, 2004, '2023-12-07 08:01:43', '2023-12-07 08:04:46', '2023-12-07 08:53:41', 52, 4.8)
, (7, 1007, 90002, 2021, '2023-12-08 20:01:39', '2023-12-08 20:06:33', '2023-12-08 20:29:51', 42, 4.9)
, (8, 1006, 90017, 2016, '2023-12-08 08:30:36', '2023-12-08 08:34:32', '2023-12-08 09:12:21', 48, 4.1)
) AS table_name(id, uuid, order_id, driver_id, order_time, start_time, finish_time, fare, grade)
)
-- 1. 数据处理: 获取等待时间,调度时间, 时间类型
SELECT
a.order_id
, a.event_time
, a.end_time
, b.order_time
, b.start_time
, (UNIX_TIMESTAMP(a.end_time) - UNIX_TIMESTAMP(a.event_time) ) AS wait_times -- 等待时间
, (UNIX_TIMESTAMP(b.start_time) - UNIX_TIMESTAMP(b.order_time)) AS schedulers_times -- 调度时间
, (CASE
WHEN HOUR(a.event_time) >= '07' AND HOUR(a.event_time) < '09' THEN "早高峰"
WHEN HOUR(a.event_time) >= '09' AND HOUR(a.event_time) < '17' THEN "工作时间"
WHEN HOUR(a.event_time) >= '17' AND HOUR(a.event_time) < '20' THEN "晚高峰"
ELSE "休息时间" END ) as time_type
FROM t_taxi_info a
JOIN t_order_info b
ON a.order_id = b.order_id
;
order_id | event_time | end_time | order_time | start_time | wait_times | schedulers_times | time_type |
---|---|---|---|---|---|---|---|
90004 | 2023-12-04 11:00:12 | 2023-12-04 11:00:42 | 2023-12-04 11:00:42 | 2023-12-04 11:05:22 | 30 | 280 | 工作时间 |
90011 | 2023-12-05 21:00:22 | 2023-12-05 21:01:06 | 2023-12-05 21:01:06 | 2023-12-05 21:04:02 | 44 | 176 | 休息时间 |
90008 | 2023-12-05 19:01:02 | 2023-12-05 19:01:52 | 2023-12-05 19:01:52 | 2023-12-05 19:08:52 | 50 | 420 | 晚高峰 |
90006 | 2023-12-06 09:01:06 | 2023-12-06 09:01:36 | 2023-12-06 09:01:36 | 2023-12-06 09:03:36 | 30 | 120 | 工作时间 |
90012 | 2023-12-06 18:01:08 | 2023-12-06 18:01:48 | 2023-12-06 18:01:48 | 2023-12-06 18:02:48 | 40 | 60 | 晚高峰 |
90018 | 2023-12-07 08:01:16 | 2023-12-07 08:01:43 | 2023-12-07 08:01:43 | 2023-12-07 08:04:46 | 27 | 183 | 早高峰 |
90002 | 2023-12-08 20:01:12 | 2023-12-08 20:01:39 | 2023-12-08 20:01:39 | 2023-12-08 20:06:33 | 27 | 294 | 休息时间 |
90017 | 2023-12-08 08:30:18 | 2023-12-08 08:30:36 | 2023-12-08 08:30:36 | 2023-12-08 08:34:32 | 18 | 236 | 早高峰 |
-- 2. 计算各时间段的叫车量,等待时间平均值,调度时间平均值
SELECT
time_type
, COUNT(order_id) AS get_taxi_num
, SUM(wait_times)/COUNT(order_id) AS wait_times_avg
, SUM(schedulers_times)/COUNT(order_id) AS schedulers_times_avg
FROM (
SELECT
a.order_id
, a.event_time
, a.end_time
, b.order_time
, b.start_time
, (UNIX_TIMESTAMP(a.end_time) - UNIX_TIMESTAMP(a.event_time) ) AS wait_times -- 等待时间
, (UNIX_TIMESTAMP(b.start_time) - UNIX_TIMESTAMP(b.order_time)) AS schedulers_times -- 调度时间
, (CASE
WHEN HOUR(a.event_time) >= '07' AND HOUR(a.event_time) < '09' THEN "早高峰"
WHEN HOUR(a.event_time) >= '09' AND HOUR(a.event_time) < '17' THEN "工作时间"
WHEN HOUR(a.event_time) >= '17' AND HOUR(a.event_time) < '20' THEN "晚高峰"
ELSE "休息时间" END ) as time_type
FROM t_taxi_info a
JOIN t_order_info b
ON a.order_id = b.order_id
) t1
GROUP BY time_type
;
time_type | get_taxi_num | wait_times_avg | schedulers_times_avg |
---|---|---|---|
休息时间 | 2 | 35.5 | 235.0 |
工作时间 | 2 | 30.0 | 200.0 |
早高峰 | 2 | 22.5 | 209.5 |
晚高峰 | 2 | 45.0 | 240.0 |