题解 | #各城市最大同时等车人数#

各城市最大同时等车人数

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

# 3.16  9:50 - 

# 字段:维度:city、 max_wait_uv
# 时间:2021年10月

# 同时等车人数: sum over? 貌似
# 要标记 envent_time算作1, end_time算作-1 即退出   这题居然是用start_time作为这个退出打车时间? 等车时间!!! 噢!!

# 等车时间: 开始打车到上车时间  或者 开始打车到取消打车这部分时间
# tb1:候车时间需要用到event_time和start_time ,
# 要构造一个time2的时间点,这个字段取order_time 和 start_time中的大值
with tb1 as(
    select gcr.uid as uid ,city,gco.order_id as order_id,event_time as time1,
    ifnull(start_time,end_time) as time2
    from tb_get_car_record gcr left join tb_get_car_order gco on gcr.order_id = gco.order_id
    where date_format(event_time,'%Y-%m')  = '2021-10'
),

# tb2:标记进入 和 离开状态
tb2 as(
    select uid,city ,time1 as all_time, 1 as label,order_id
    from tb1
    union all
    select uid,city ,time2 as all_time, -1 as label,order_id
    from tb1
    order by all_time asc, label desc
),
# tb3:计算累计
tb3 as(
select city ,all_time, 
# sum(label) over(partition by city,date(all_time) order by all_time) as wait_uv
sum(label) over(order by all_time,label desc) as wait_uv 
# 注意,这里的累加,不用分组,只用order by时间和标记字段label即可,分组功能下一步用group by进行 
from tb2

)
# # 求取单日各城市最大同时等车人数
select city,max(wait_uv) as max_wait_uv
from tb3
group by city,date(all_time)
order by max(wait_uv) ,city 

# 注意,这里的累加,不用分组,只用order by时间和标记字段label即可,分组功能下一步用group by进行 
# 注意,用date_format的时候,等于的值要用单引号括起来''





全部评论

相关推荐

牛客583549203号:腾讯还好,况且实习而已,实习生流动性很大,属于正常现象,记得和HR委婉解释
点赞 评论 收藏
分享
03-31 18:02
门头沟学院 Java
白日梦想家_等打包版:不要的哦佛给我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务