题解 | #SQL 21.每个城市中评分最高的司机信息#

每个城市中评分最高的司机信息

http://www.nowcoder.com/practice/dcc4adafd0fe41b5b2fc03ad6a4ac686

每个城市中评分最高的司机日均接单量和里程数

明确题意:

统计每个城市中评分最高的司机平均评分、日均接单量和日均行驶里程数。有多个司机评分并列最高时,都输出。

平均评分和日均接单量保留1位小数,日均行驶里程数保留3位小数,按日均接单数升序排序。


问题分解:

  • 计算每个司机的评分、日均接单量、日均里程和城市内评分排名(生成子表t_driver_rk):
    • 计算每个司机的评分等信息(生成子表t_driver_info):
      • 关联打车记录表和订单表:tb_get_car_record JOIN tb_get_car_order USING(order_id)
      • 按司机和城市分组:GROUP BY 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
    • 计算日均指标和排名:
      • 日均订单量:order_num / work_days as avg_order_num
      • 日均里程数:toal_mileage / work_days as avg_mileage
      • 计算城市内的评分排名,允许并列第一:RANK() over(PARTITION BY city ORDER BY avg_grade DESC) as rk
      • 保留小数位数:ROUND(x, 1)
  • 筛选每个城市的第一名:WHERE rk = 1

细节问题:

  • 表头重命名:as
  • 按日均接单数升序排序:ORDER BY avg_order_num;

完整代码:

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;
SQL大厂真题 文章被收录于专栏

大厂真题手把手教你怎么解~

全部评论
话说题目是不是说的太不清楚了,什么叫评分最高的司机的平均评分,这到底是按评分排名还是平均评分排名,牛客天天搁这含糊不清
6 回复 分享
发布于 2022-02-22 16:48
还是大佬写的简洁 思路清晰 我自己虽然能做出来 不过很乱
点赞 回复 分享
发布于 2022-01-29 16:23
请教下大佬我这个写法有什么问题呢? select city , driver_id ,round(g,1),round(g/c,1),round(m/c,3) from( SELECT city, driver_id, count(a.order_time), sum(mileage) m, avg(grade) g, dense_rank () over ( PARTITION BY city ORDER BY avg(grade) DESC ) rk, count(distinct DATE_FORMAT(order_time,'%Y%m%d')) c FROM tb_get_car_order a LEFT JOIN tb_get_car_record b ON a.order_id = b.order_id GROUP BY city, driver_id) a where rk = 1 group by city , driver_id
点赞 回复 分享
发布于 2022-04-04 22:05
想问下join关联用uid为什么会报错?
点赞 回复 分享
发布于 2022-07-15 16:58
这里的评分最高,应该指的是总分最高吧。如果是这样,那么平均分高的不一定总分最高,那么就不应该用avg_grade来确定第一名。能给解答嘛。
点赞 回复 分享
发布于 2023-02-25 17:39 广东
请教下我的写法有什么问题呢?select city,driver_id,avg_grade,avg_order_num,avg_mileage from(select city,driver_id, round(avg(grade),1) as avg_grade, round(count(order_id)/count(distinct date(order_time)),1) as avg_order_num, round(sum(mileage)/count(distinct date(order_time)),3) as avg_mileage, rank()over(partition by city order by round(avg(grade),1) desc) as rn from tb_get_car_record t1 join tb_get_car_order t2 using(order_id) group by city,driver_id order by avg_grade desc )as a where rn = 1 order by avg_order_num
点赞 回复 分享
发布于 2023-03-09 10:20 广东

相关推荐

09-25 10:34
东北大学 Java
多面手的小八想要自然醒:所以读这么多年到头来成为时代车轮底下的一粒尘
点赞 评论 收藏
分享
Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
44 2 评论
分享
牛客网
牛客企业服务