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

各城市最大同时等车人数

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

with 
    t1 as(
        #1、开始等车
        select
            city,
            event_time ,
            1 `flag`
        from tb_get_car_record 
        where date(event_time) like '2021-10%'
    
    ),
    t2 as(
        #2、退出等车
        select
            city,
            if(start_time is null,finish_time,start_time) ,
            -1 `flag`
        from tb_get_car_record tgcr
        join tb_get_car_order tgco
        on tgcr.order_id=tgco.order_id
        where date(event_time) like '2021-10%'
    )
select 
    city,
    max(wait_uv) `max_wait_uv`
from(
    select 
        city,
        event_time,
    sum(flag) over(partition by city,date(event_time) order by event_time,flag desc ) `wait_uv`
    from(
        select 
            *
        from t1
        union all
        select 
            *
        from t2
    )t3
)t4
group by city
order by max_wait_uv,city

#考虑到有重复的数据就要用union all
#由于等车人数记作先增加后减少,所以对flag要降序

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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