# 平均接单数:司机总接单数/司机数,打车订单表有多少条记录就接单多少次 # 平均兼职收入:总费用/司机数 # 计算平均接单数和平均兼职收入 select (select "北京") city,round(count(o.id)/count(distinct driver_id),3) avg_order_num, round(sum(fare)/count(distinct driver_id),3) avg_income from tb_get_car_order o left join tb_get_car_record r on r.order_id = o.order_id where driver_id in ( # 统计2021年国庆7天期间在北京市接单至少3次的司机 select o1.driver_id from tb_get_car_record r1 join tb_get_car_order o1 on r1.order_id = o1.order_id where city = "北京" and date(order_time) between '2021-10-01' and '2021-10-07' group by o1.driver_id having count(o1.driver_id) >= 3 ) and order_time >= "2021-10-01 00:00:00" and finish_time < "2021-10-08 00:00:00" and city = "北京"
这个代码比较繁复,未优化,但思路没问题。
对于日期函数的使用date()函数接受一个日期时间值作为参数,并返回该日期时间值的日期部分。