题解 | #每篇文章同一时刻最大在看人数#

每篇文章同一时刻最大在看人数

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

思路:

  1. 先对用户行为打标,进入为1,离开为-1
  • 进入表
  • 出去表
  • union all联结两表
  1. 按照时间,顺序,累加用户状态,可得到用户同时在线人数。
  • 同时在线人数
    • sum(tag) over(partition by artical_id order by dt,tag desc) -- 先记录增加,后记录减少,所以要tag desc
    • 条件,artical_id!=0
  • 取最大,并排序
    • artical_id 分组
    • 排序
      select
      artical_id,
      max(uv) as max_uv
      from(
      select 
        artical_id,
        dt,
        sum(tag) over(partition by artical_id order by dt,tag desc) as uv
      from(
        select
            artical_id,
            in_time as dt,
            1 as tag
        from tb_user_log
        union all
        select
            artical_id,
            out_time as dt,
            -1 as tag
        from tb_user_log
      ) t1
      where artical_id!=0
      ) t2
      group by artical_id
      order by max_uv desc
全部评论

相关推荐

07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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