• 【力扣白嫖日记】601.体育馆的人流量


    前言

    练习sql语句,所有题目来自于力扣(https://leetcode.cn/problemset/database/)的免费数据库练习题。

    今日题目:

    601.体育馆的人流量
    表:Stadium

    列名类型
    idint
    visit_datedate
    peopleint

    编写解决方案找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。

    返回按 visit_date 升序排列 的结果表。


    我那不值一提的想法:

    • 我的思路:首先梳理表内容,题干一共给了一张表,记录了序号id,日期和人流量。
    • 其次分析需求,需要找到每行的人数大于或等于100且id连续的三行或更多的记录。
    • 针对于连续出现的次数的方法,最简单的方法就是自连接,连续出现三行就自连接三次,但是不够灵活,如果需要连续出现100行以上的数据就很难去计算了,这里我们可以使用row_number()去灵活的查询,以前也做过类似的题

    https://blog.csdn.net/dkmaa/article/details/136302362?spm=1001.2014.3001.5506

    • 针对于这道题,我的想法是需要先找到所有>100的数据,在这里我把所有大于100的数据设置为0,方便以后partition by
    with onehund_people as (
        select id,people,
        case when people >=100 then 0
        else people 
        end as peo
        from Stadium
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 其次使用id - row_number() over(partition by peo order by id),得到它们的差值,至于为何这样,引用里面的文章,我已经详细说明过了,这里就不多说了。
    select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
    from onehund_people o 
    left join Stadium s 
    on o.id = s.id 
    order by o.id 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 在得到所有的差值后,我们对其分组求数量,数量>=3,便是我们需要的数据
    select num 
        from (
            select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
            from onehund_people o 
            left join Stadium s 
            on o.id = s.id 
            order by o.id 
        ) as a 
    group by peo,num 
    having count(*) >=3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 现在我们已经得到了连续出现3次的num,那么现在再嵌套一层查询,使num在连续出现三次的num里面,同时注意,我们并没有筛选>100 的数据,所以末尾加个条件peo = 0
    select id,visit_date,people
    from (
        select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
        from onehund_people o 
        left join Stadium s 
        on o.id = s.id 
        order by o.id 
    ) b 
    where num in
        (select num 
        from (
            select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
            from onehund_people o 
            left join Stadium s 
            on o.id = s.id 
            order by o.id 
        ) as a 
        group by peo,num 
        having count(*) >=3
    )
    and peo = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 完整代码
    with onehund_people as (
        select id,people,
        case when people >=100 then 0
        else people 
        end as peo
        from Stadium
    )
    select id,visit_date,people
    from (
        select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
        from onehund_people o 
        left join Stadium s 
        on o.id = s.id 
        order by o.id 
    ) b 
    where num in
        (select num 
        from (
            select o.id,s.visit_date,peo,s.people,o.id - row_number() over(partition by peo order by o.id) as num
            from onehund_people o 
            left join Stadium s 
            on o.id = s.id 
            order by o.id 
        ) as a 
        group by peo,num 
        having count(*) >=3
    )
    and peo = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 我这个答案其实写复杂了,有更简单的写法,但是方法是一样的,这里就这样了,能运行就行。

    结果:

    在这里插入图片描述


    总结:

    能运行就行。


  • 相关阅读:
    iwemeta元宇宙:特斯拉CEO马斯克未来10年,卖出1亿特斯拉。你们认为可以吗?
    华为机试真题 C++ 实现【信道分配】
    Http请求get与post请求方式的各种相关面试总结
    String的使用
    网络安全岗位介绍——售前工程师
    Spark - 第15章 Spark如何在集群上运行
    docker 安卓部署RabbitMQ
    js节流和防抖
    面试官:说一说你的第一个Java程序是怎么跑起来的
    【附源码】计算机毕业设计JAVA合租吧管理系统
  • 原文地址:https://blog.csdn.net/dkmaa/article/details/136840132