题解 | #国庆期间每类视频点赞量和转发量#

国庆期间每类视频点赞量和转发量

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

WITH
    iinfo AS (
        SELECT
            video_id,
            DATE_FORMAT (end_time, '%Y-%m-%d') AS dt,
            SUM(if_like) AS if_like,
            SUM(if_retweet) AS if_retweet
        FROM
            tb_user_video_log
        GROUP BY
            video_id,
            DATE_FORMAT (end_time, '%Y-%m-%d')
    )
select
    *
from
    (
        SELECT
            info.tag,
            uv.dt,
            SUM(uv.if_like) OVER (
                PARTITION BY
                    info.tag
                ORDER BY
                    uv.dt ROWS BETWEEN 6 PRECEDING
                    AND CURRENT ROW
            ) AS sum_like_cnt_7d,
            MAX(uv.if_retweet) OVER (
                PARTITION BY
                    info.tag
                ORDER BY
                    uv.dt ROWS BETWEEN 6 PRECEDING
                    AND CURRENT ROW
            ) AS max_retweet_cnt_7d
        FROM
            iinfo uv
            LEFT JOIN tb_video_info info ON uv.video_id = info.video_id
    ) b
WHERE
    dt BETWEEN '2021-10-01' AND '2021-10-03'
order by
    tag desc,
    dt
  1. 解决滑动窗口问题 先需要求出每个日期的完整前七日信息 之后合并后再进行一次Filter Date
  2. 求向上滑动的方法

#1. ROWS 滑动 ROWS BETWEEN and Current Row
SUM(uv.if_like) OVER (
                PARTITION BY info.tag
  				ORDER BY uv.dt ROWS BETWEEN 6 PRECEDING AND CURRENT ROW 
            ) AS sum_like_cnt_7d,
			
#2. Range 滑动
Sum() over ( order by uv.dt range between interval '7 Day' AND CURRENT ROW )

全部评论

相关推荐

头像
10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
牛客618272644号:佬携程工作怎么样,强度大吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务