题解 | #国庆期间每类视频点赞量和转发量#
国庆期间每类视频点赞量和转发量
http://www.nowcoder.com/practice/f90ce4ee521f400db741486209914a11
# "前7天"核心就在窗口函数: ORDER BY t1.dt ROWS 6 PRECEDING # 默认有current row # order by 当天跟前6天, 即partition by +分组;order by +窗口范围 SELECT * FROM ( SELECT t1.tag,t1.dt, SUM(t1.like_cnt) OVER (PARTITION BY t1.tag ORDER BY t1.dt ROWS 6 PRECEDING) sum_like_cnt_7d, MAX(t1.retweet_cnt) OVER (PARTITION BY t1.tag ORDER BY t1.dt ROWS 6 PRECEDING ) max_retweet_cnt_7d FROM # 先求出每个tag下每天的like跟retweet量;题目下面还给了示例 ( SELECT tag, DATE(start_time) dt, SUM(if_like) like_cnt, SUM(if_retweet) retweet_cnt FROM tb_user_video_log a JOIN tb_video_info b ON a.video_id = b.video_id GROUP BY b.tag, DATE(start_time) ) t1 ) t2 WHERE t2.dt BETWEEN "2021-10-01" AND "2021-10-03" #在最后的时候,dt才只保留这三天;其他时候都是所有的date放进窗口函数一起计算的;记得要加这一层 ORDER BY t2.tag DESC,t2.dt