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

各城市最大同时等车人数

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

with df as(
    #进入等车状态 uv+1
    SELECT 
    city,
    event_time as "UV变动时间点",
    1 as "UV"
    FROM tb_get_car_record
    UNION ALL 
    #退出等车_情况一 uv-1
    SELECT 
    city,
    end_time as "UV变动时间点",
    -1 as "UV"
    from tb_get_car_record
    where order_id is null
    UNION all       
    #退出等车_情况二 uv-1
    SELECT 
    df2.city,
    ifnull(df1.start_time,df1.finish_time) as "UV变动时间点",
    -1 as "UV"
    from tb_get_car_order as df1
    left join tb_get_car_record as df2
    on df1.order_id = df2.order_id
)

select city,max(UV_num) as "最大瞬时UV" from(
    select
    city,
    sum(UV) over(partition by city order by UV变动时间点 asc,UV desc ROWS between unbounded preceding and current row) as "UV_num"  #题目要求同一时刻先增后减,所以排序需要追加 UV desc;ROWS between ...之后都可以省略,此处为了防止本人忘记,就不省略了
    from df
    where date_format(UV变动时间点,'%Y-%m') = '2021-10'
) as a
group by city
order by 最大瞬时UV asc,city asc

附带注释

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务