# 1. 统计每一天的点赞和转发量
# 2. 统计截止到当前日期每天的总点赞量和最大转发量
# 3. 从上面筛选出所需要的那三天数据
# 重要知识点:滑动开窗
select
tag,
dt,
sum_like_cnt_7d,
max_retweet_cnt_7d
from (
# 2.统计截止到当前日期每天的总点赞量和最大转发量
select
tag,
dt,
sum(like_cnt)over(partition by tag order by dt rows between 6 preceding and current row) as sum_like_cnt_7d,
max(retweet_cnt)over(partition by tag order by dt rows between 6 preceding and current row) as max_retweet_cnt_7d
from(
# 1.统计每一天的点赞和转发量
select
tag,
date(start_time) dt,
sum(if_like) like_cnt,
sum(if_retweet) retweet_cnt
from tb_user_video_log
left join tb_video_info using(video_id)
group by tag,date(start_time)
order by tag,date(start_time)
) t1
) t2
where dt between '2021-10-01' and '2021-10-03' # 日期条件一定是在子查询t1完成之后再筛选,否则漏数据
order by tag desc,dt