列表

详情


SQL174. 2021年国庆在北京接单3次及以上的司机统计信息

描述

用户打车记录表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
(uid-用户ID, city-城市, event_time-打车时间, end_time-打车结束时间, order_id-订单号)


打车订单表tb_get_car_order
id order_id uid driver_id order_time start_time finish_time mileage fare grade
1 9002 101 201 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
2021-10-01 08:18:00
2021-10-01 08:31:00
15 44 5
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
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
(order_id-订单号, uid-用户ID, driver_id-司机ID, order_time-接单时间, start_time-开始计费的上车时间,  finish_time-订单完成时间, mileage-行驶里程数, fare-费用, grade-评分)


场景逻辑说明
  • 用户提交打车请求后,在用户打车记录表生成一条打车记录,order_id-订单号设为null

  • 当有司机接单时,在打车订单表生成一条订单,填充order_time-接单时间及其左边的字段,start_time-开始计费的上车时间及其右边的字段全部为null,并把order_id-订单号order_time-接单时间end_time-打车结束时间)写入打车记录表;若一直无司机接单,超时或中途用户主动取消打车,则记录end_time-打车结束时间

  • 若乘客上车前,乘客或司机点击取消订单,会将打车订单表对应订单的finish_time-订单完成时间填充为取消时间,其余字段设为null

  • 当司机接上乘客时,填充订单表中该start_time-开始计费的上车时间
  • 当订单完成时填充订单完成时间、里程数、费用;评分设为null,在用户给司机打1~5星评价后填充。


问题:请统计2021年国庆7天期间在北京市接单至少3次的司机的平均接单数和平均兼职收入(暂不考虑平台佣金,直接计算完成的订单费用总额),结果保留3位小数。

输出示例
示例数据的输出结果如下
city avg_order_num avg_income
北京 3.500 121.000
解释:
2021年国庆期间北京市的订单中,202共接了3单,兼职收入105;203接了4单,兼职收入137;201共接了1单,但取消了; 接单至少3次的司机有202和203,他两人全部总共接单数为7,总收入为242。因此平均接单数为3.500,平均收入为121.000;

示例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 DOUBLE COMMENT '行驶里程数',
    fare DOUBLE 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) ;

INSERT INTO tb_get_car_order(order_id, uid, driver_id, order_time, start_time, finish_time, mileage, fare, grade) VALUES
 (9002, 101, 201, '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', '2021-10-01 08:18:00', '2021-10-01 08:31:00', 15.0, 44, 5),
 (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),
 (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);

输出:

北京|3.500|121.000

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 36ms, 内存消耗: 6500KB, 提交时间: 2022-01-25

SELECT "北京" as city, ROUND(AVG(order_num), 3) as avg_order_num,
    ROUND(AVG(income), 3) as avg_income
FROM (
    SELECT driver_id, COUNT(order_id) as order_num, SUM(fare) as income
    FROM tb_get_car_order
    JOIN tb_get_car_record USING(order_id)
    WHERE city = "北京" and DATE_FORMAT(order_time,"%Y%m%d") BETWEEN '20211001' AND '20211007'
    GROUP BY driver_id
    HAVING COUNT(order_id) >= 3
) as t_driver_info;


       

Mysql 解法, 执行用时: 37ms, 内存消耗: 6320KB, 提交时间: 2022-01-24

# select a.city as city,b.driver_id as driver_id,count(1) as order_num,sum(b.fare) as income from 
# tb_get_car_record as a inner join tb_get_car_order as b 
# on a.order_id = b.order_id 
# where date(b.order_time) between '2021-10-01' and '2021-10-07' and city = '北京'
# group by b.driver_id 
# having order_num>=3;

select a.city,round(sum(a.order_num)/count(a.order_num),3) as avg_order_num,
round(sum(a.income)/count(a.income),3) as avg_income from 
(select a.city as city,b.driver_id as driver_id,count(1) as order_num,sum(b.fare) as income from 
tb_get_car_record as a inner join tb_get_car_order as b 
on a.order_id = b.order_id 
where date(b.order_time) between '2021-10-01' and '2021-10-07' and city = '北京'
group by b.driver_id 
having order_num>=3) as a 
group by a.city;

Mysql 解法, 执行用时: 37ms, 内存消耗: 6388KB, 提交时间: 2022-02-09

SELECT city, round(avg(cnt), 3) as avg_order_num,
    round(avg(sum_fare), 3) as avg_income
FROM (
    SELECT city, driver_id, count(t1.order_id) as cnt,
        sum(fare) as sum_fare
    FROM tb_get_car_order as t1 JOIN tb_get_car_record as t2
    ON t1.order_id = t2.order_id
    WHERE date(order_time) between '2021-10-01' and '2021-10-07'
    AND city = '北京'
    GROUP BY city, driver_id
    HAVING cnt >= 3
) as t
GROUP BY city

Mysql 解法, 执行用时: 37ms, 内存消耗: 6392KB, 提交时间: 2022-01-25

SELECT
    city,
    ROUND(AVG(order_cnt), 3) AS avg_order_num,
    ROUND(SUM(income_amount) / COUNT(driver_id), 3) AS avg_income
FROM
(SELECT
    city,
    driver_id,
    COUNT(t1.order_id) AS order_cnt,
    SUM(fare) AS income_amount
FROM tb_get_car_order AS t1
JOIN tb_get_car_record AS t2
ON t1.order_id = t2.order_id
WHERE
    DATE(order_time) BETWEEN '2021-10-01' AND '2021-10-07'
    AND
    city = '北京'
GROUP BY city, driver_id
HAVING order_cnt >= 3) AS t3
GROUP BY city;

Mysql 解法, 执行用时: 37ms, 内存消耗: 6392KB, 提交时间: 2021-12-18

select max(city), 
       round(avg(order_num), 3) as avg_order_num,
       round(avg(total_fare), 3) as avg_income
from (select city, driver_id, count(tb_get_car_order.order_id) as order_num, sum(fare) as total_fare
      from tb_get_car_record inner join tb_get_car_order
      on tb_get_car_record.order_id = tb_get_car_order.order_id
      where city = "北京" and date(event_time) between date("2021-10-01") and date("2021-10-07")
      group by city, driver_id
      having count(tb_get_car_order.order_id) >= 3) as t1

上一题