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

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

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

#不需要什么思路,应该都懂,纯纯为了打个卡。只需要注意平均计算评分的平均值的时候:评分值不为空的记录 才能计算进去,和上题保持一致

select
    d.city,d.driver_id,d.avg_grade,d.avg_order_num,d.avg_mileage
from 
(
    select

        c.city,
        c.driver_id,
        format(c.allGrades/c.gradeNums,1) as avg_grade,
        format(c.orderNum/c.days,1) as avg_order_num,
        format(c.miles/c.days,3) as avg_mileage,
        dense_rank() over(partition by c.city order by c.allGrades/c.gradeNums desc ) as rk
    from 
    (

        select

            a.driver_id,
            max(b.city) as city,
            count(1) as orderNum,
            count(distinct date_format(a.order_time,'%Y-%m-%d')) as days,
            sum(case when a.start_time is not null and a.finish_time is not null and a.grade is not null then 1 else 0 end) as gradeNums,
            sum(a.grade) as allGrades,
            sum(a.mileage) as miles
        from tb_get_car_order a join tb_get_car_record b on a.order_id = b.order_id
        group by a.driver_id
    ) c
)d where d.rk = 1
order by d.avg_order_num

全部评论

相关推荐

头像
11-26 15:46
已编辑
中南大学 后端
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务