SQL176. 每个城市中评分最高的司机信息
描述
用户打车记录表tb_get_car_record
id | uid | city | event_time | end_time | order_id |
1 | 101 | 北京 | 2021-10-01 07:00:00 | 2021-10-01 07:02:00 | NULL |
2 | 102 | 北京 | 2021-10-01 09:00:30 | 2021-10-01 09:01:00 | 9001 |
3 | 101 | 北京 | 2021-10-01 08:28:10 | 2021-10-01 08:30:00 | 9002 |
4 | 103 | 北京 | 2021-10-02 07:59:00 | 2021-10-02 08:01:00 | 9003 |
5 | 104 | 北京 | 2021-10-03 07:59:20 | 2021-10-03 08:01:00 | 9004 |
6 | 105 | 北京 | 2021-10-01 08:00:00 | 2021-10-01 08:02:10 | 9005 |
7 | 106 | 北京 | 2021-10-01 17:58:00 | 2021-10-01 18:01:00 | 9006 |
8 | 107 | 北京 | 2021-10-02 11:00:00 | 2021-10-02 11:01:00 | 9007 |
9 | 108 | 北京 | 2021-10-02 21:00:00 | 2021-10-02 21:01:00 | 9008 |
10 | 109 | 北京 | 2021-10-08 18:00:00 | 2021-10-08 18:01:00 | 9009 |
打车订单表tb_get_car_order
id | order_id | uid | driver_id | order_time | start_time | finish_time | mileage | fare | grade |
1 | 9002 | 101 | 202 | 2021-10-01 08:30:00 | NULL | 2021-10-01 08:31:00 | NULL | NULL | NULL |
2 | 9001 | 102 | 202 | 2021-10-01 09:01:00 | 2021-10-01 09:06:00 | 2021-10-01 09:31:00 | 10 | 41.5 | 5 |
3 | 9003 | 103 | 202 | 2021-10-02 08:01:00 | 2021-10-02 08:15:00 | 2021-10-02 08:31:00 | 11 | 41.5 | 4 |
4 | 9004 | 104 | 202 | 2021-10-03 08:01:00 | 2021-10-03 08:13:00 | 2021-10-03 08:31:00 | 7.5 | 22 | 4 |
5 | 9005 | 105 | 203 | 2021-10-01 08:02:10 | NULL | 2021-10-01 08:31:00 | NULL | NULL | NULL |
6 | 9006 | 106 | 203 | 2021-10-01 18:01:00 | 2021-10-01 18:09:00 | 2021-10-01 18:31:00 | 8 | 25.5 | 5 |
7 | 9007 | 107 | 203 | 2021-10-02 11:01:00 | 2021-10-02 11:07:00 | 2021-10-02 11:31:00 | 9.9 | 30 | 5 |
8 | 9008 | 108 | 203 | 2021-10-02 21:01:00 | 2021-10-02 21:10:00 | 2021-10-02 21:31:00 | 13.2 | 38 | 4 |
9 | 9009 | 109 | 203 | 2021-10-08 18:01:00 | 2021-10-08 18:11:50 | 2021-10-08 18:51:00 | 13 | 40 | 5 |
用户提交打车请求后,在用户打车记录表生成一条打车记录,order_id-订单号设为null;
当有司机接单时,在打车订单表生成一条订单,填充order_time-接单时间及其左边的字段,start_time-开始计费的上车时间及其右边的字段全部为null,并把order_id-订单号和order_time-接单时间(end_time-打车结束时间)写入打车记录表;若一直无司机接单,超时或中途用户主动取消打车,则记录end_time-打车结束时间。
若乘客上车前,乘客或司机点击取消订单,会将打车订单表对应订单的finish_time-订单完成时间填充为取消时间,其余字段设为null。
city | driver_id | avg_grade | avg_order_num | avg_mileage |
北京 | 203 | 4.8 | 1.7 | 14.700 |
示例1
输入:
DROP TABLE IF EXISTS tb_get_car_record,tb_get_car_order; CREATE TABLE tb_get_car_record ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid INT NOT NULL COMMENT '用户ID', city VARCHAR(10) NOT NULL COMMENT '城市', event_time datetime COMMENT '打车时间', end_time datetime COMMENT '打车结束时间', order_id INT COMMENT '订单号' ) CHARACTER SET utf8 COLLATE utf8_bin; CREATE TABLE tb_get_car_order ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', order_id INT NOT NULL COMMENT '订单号', uid INT NOT NULL COMMENT '用户ID', driver_id INT NOT NULL COMMENT '司机ID', order_time datetime COMMENT '接单时间', start_time datetime COMMENT '开始计费的上车时间', finish_time datetime COMMENT '订单结束时间', mileage FLOAT COMMENT '行驶里程数', fare FLOAT COMMENT '费用', grade TINYINT COMMENT '评分' ) CHARACTER SET utf8 COLLATE utf8_bin; INSERT INTO tb_get_car_record(uid, city, event_time, end_time, order_id) VALUES (101, '北京', '2021-10-01 07:00:00', '2021-10-01 07:02:00', null), (102, '北京', '2021-10-01 09:00:30', '2021-10-01 09:01:00', 9001), (101, '北京', '2021-10-01 08:28:10', '2021-10-01 08:30:00', 9002), (103, '北京', '2021-10-02 07:59:00', '2021-10-02 08:01:00', 9003), (104, '北京', '2021-10-03 07:59:20', '2021-10-03 08:01:00', 9004), (105, '北京', '2021-10-01 08:00:00', '2021-10-01 08:02:10', 9005), (106, '北京', '2021-10-01 17:58:00', '2021-10-01 18:01:00', 9006), (107, '北京', '2021-10-02 11:00:00', '2021-10-02 11:01:00', 9007), (108, '北京', '2021-10-02 21:00:00', '2021-10-02 21:01:00', 9008), (109, '北京', '2021-10-08 18:00:00', '2021-10-08 18:01:00', 9009); INSERT INTO tb_get_car_order(order_id, uid, driver_id, order_time, start_time, finish_time, mileage, fare, grade) VALUES (9002, 101, 202, '2021-10-01 08:30:00', null, '2021-10-01 08:31:00', null, null, null), (9001, 102, 202, '2021-10-01 09:01:00', '2021-10-01 09:06:00', '2021-10-01 09:31:00', 10.0, 41.5, 5), (9003, 103, 202, '2021-10-02 08:01:00', '2021-10-02 08:15:00', '2021-10-02 08:31:00', 11.0, 41.5, 4), (9004, 104, 202, '2021-10-03 08:01:00', '2021-10-03 08:13:00', '2021-10-03 08:31:00', 7.5, 22, 4), (9005, 105, 203, '2021-10-01 08:02:10', null, '2021-10-01 08:31:00', null, null, null), (9006, 106, 203, '2021-10-01 18:01:00', '2021-10-01 18:09:00', '2021-10-01 18:31:00', 8.0, 25.5, 5), (9007, 107, 203, '2021-10-02 11:01:00', '2021-10-02 11:07:00', '2021-10-02 11:31:00', 9.9, 30, 5), (9008, 108, 203, '2021-10-02 21:01:00', '2021-10-02 21:10:00', '2021-10-02 21:31:00', 13.2, 38, 4), (9009, 109, 203, '2021-10-08 18:01:00', '2021-10-08 18:11:50', '2021-10-08 18:51:00', 13, 40, 5);
输出:
北京|203|4.8|1.7|14.700
Mysql 解法, 执行用时: 37ms, 内存消耗: 6376KB, 提交时间: 2022-01-25
# 问题:请统计每个城市中评分最高的司机平均评分、日均接单量和日均行驶里程数。 # 注:有多个司机评分并列最高时,都输出。 # 平均评分和日均接单量保留1位小数, # 日均行驶里程数保留3位小数,按日均接单数升序排序。 with c as( SELECT driver_id FROM( SELECT *,rank() over(partition by city order by avg_grade desc) as r FROM( SELECT city,driver_id,avg(grade) as avg_grade FROM tb_get_car_order tbgco JOIN tb_get_car_record tbgcr USING(order_id) GROUP BY city,driver_id) as a) as b WHERE r = 1) SELECT city,tbgco.driver_id, round(avg(grade),1) as avg_grade, round(count(order_time) / count(distinct DATE(order_time)),1) as avg_order_num, sum(mileage) / count(distinct DATE(order_time)) as avg_mileage FROM tb_get_car_order tbgco JOIN tb_get_car_record tbgcr USING(order_id) WHERE tbgco.driver_id in (select * from c) GROUP BY city,tbgco.driver_id order by avg_order_num
Mysql 解法, 执行用时: 37ms, 内存消耗: 6384KB, 提交时间: 2021-12-27
select city,driver_id,avg_grade,avg_order_num,avg_mileage from (select city,driver_id,avg_grade,avg_order_num,avg_mileage, dense_rank() over(partition by city order by avg_grade desc) as rank_grade from (select city,driver_id,round(avg(grade),1)avg_grade, round(count(ord.order_id)/count(distinct date_format(order_time,"%Y-%m-%d")),1)avg_order_num, round(sum(mileage)/count(distinct date_format(order_time,"%Y-%m-%d")),3)avg_mileage from tb_get_car_order ord left join tb_get_car_record re on ord.order_id=re.order_id group by city,driver_id)a )b where rank_grade=1 order by avg_order_num;
Mysql 解法, 执行用时: 37ms, 内存消耗: 6384KB, 提交时间: 2021-12-06
select city,driver_id,avg_grade,avg_order_num,avg_mileage from (select r.city,o.driver_id, round(avg(o.grade),1) avg_grade, round(count(o.order_id)/(count(distinct date(order_time))),1) avg_order_num, round(sum(o.mileage)/(count(distinct date(order_time))),3) avg_mileage, dense_rank() over (partition by r.city order by round(avg(o.grade),1) desc) t_rank from tb_get_car_order o inner join tb_get_car_record r on r.order_id=o.order_id group by r.city,o.driver_id) as a where t_rank = 1 order by avg_order_num
Mysql 解法, 执行用时: 37ms, 内存消耗: 6408KB, 提交时间: 2022-01-22
SELECT city, driver_id, avg_grade, avg_order_num, avg_mileage FROM ( SELECT city, driver_id, ROUND(avg_grade, 1) as avg_grade, ROUND(order_num / work_days, 1) as avg_order_num, ROUND(toal_mileage / work_days, 3) as avg_mileage, RANK() over(PARTITION BY city ORDER BY avg_grade DESC) as rk FROM ( SELECT driver_id, city, AVG(grade) as avg_grade, COUNT(DISTINCT DATE(order_time)) as work_days, COUNT(order_time) as order_num, SUM(mileage) as toal_mileage FROM tb_get_car_record JOIN tb_get_car_order USING(order_id) GROUP BY driver_id, city ) as t_driver_info ) as t_driver_rk WHERE rk = 1 ORDER BY avg_order_num;
Mysql 解法, 执行用时: 37ms, 内存消耗: 6412KB, 提交时间: 2021-12-19
select t.city, t.driver_id, t.avg_grade, t.avg_order_num, t.avg_mileage from (select r.city as city, o.driver_id as driver_id, round(avg(o.grade) ,1) as avg_grade, round(count(*)/count(distinct date(finish_time)),1)as avg_order_num, round(sum(o.mileage)/count(distinct date(finish_time)),3)as avg_mileage, rank() over (partition by r.city order by round(avg(o.grade) ,1) desc) as ranking from tb_get_car_order o join tb_get_car_record r using(order_id) group by r.city, o.driver_id) as t where t.ranking=1 order by t.avg_order_num