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

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

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

select
    artical_id,
    max(cnt) as max_uv
from
    (
        select
            artical_id,
            dt,
            SUM(diff) OVER (
                PARTITION BY
                    artical_id
                ORDER BY
                    dt,diff desc
            ) as cnt
        from
            (
                select
                    artical_id,
                    in_time as dt,
                    1 as diff
                from
                    tb_user_log
                where
                    artical_id != 0
                union all
                select
                    artical_id,
                    out_time as dt,
                    -1 as diff
                from
                    tb_user_log
                where
                    artical_id != 0
            ) t1
    ) t2
group by
    artical_id
order by max_uv desc;
#  这道题是看了题解才写出来的,但是这个思路学到了。

全部评论

相关推荐

09-27 14:42
已编辑
浙江大学 Java
未来未临:把浙大放大加粗就行
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务