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

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

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

select city,driver_id,avg_grade,avg_order_num,avg_mileage from
(select 
city,
driver_id,
round(avg_grade,1)avg_grade,
round(order_num/workdays,1) as avg_order_num,
round(sum_mileage/workdays,3) as avg_mileage,
rank()over(partition by city order by avg_grade desc) as rk
from 
  (select
  city,
  driver_id,
  avg(grade) as avg_grade,
  count(order_id)as order_num,
  sum(mileage) as sum_mileage,
  count(distinct date(finish_time))as workdays
  from tb_get_car_record
  join tb_get_car_order using(order_id)
  group by driver_id,city) as tc) as td 
where rk=1
order by avg_order_num

千辛万苦写出来了,都是写的过程中知道要一层一层嵌套。本题关键在于窗口函数rank(),因为要求求每个城市中平均得分最高的数据,所以可以用到窗口函数,还要多加理解窗口函数。这里只是刚好只有北京的数据,所以只显示了北京,正常可能有其他的城市的rank1 显示出来

全部评论

相关推荐

Bug压路:老哥看得出来你是想多展示一些项目,但好像一般最多两个就够了😂页数一般一页,多的也就2页;这些项目应该是比较同质化的,和评论区其他大佬一样,我也觉得应该展示一些最拿手的(质量>数量)😁😁😁专业技能部分也可以稍微精简一些
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务