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

明确题意:

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


问题分解:

  • 计算2021国庆在北京接单至少3次的司机信息:
    • 关联接单表和打车记录表: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'
    • 按司机ID分组:GROUP BY driver_id
    • 统计每个司机的接单量和接单收入:COUNT(order_id) as order_num, SUM(fare) as income
    • 筛选接单至少3次的分组(司机):HAVING COUNT(order_id) >= 3
  • 基于上述结果,统计他们的平均接单量和平均收入:
    • 平均接单量:AVG(order_num) as avg_order_num
    • 平均收入:AVG(income) as avg_income
  • 保留3位小数:ROUND(x, 3)

细节问题:

  • 表头重命名:as

完整代码:

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;