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

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

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 )

全部评论

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务