1、问题:统计在有用户互动的最近一个月(按包含当天在内的近30天算,比如10月31日的近30天为10.2~10.31之间的数据)中,每类视频的转发量和转发率(保留3位小数)。
注:转发率=转发量÷播放量。结果按转发率降序排序。
select
tag,
sum(if_retweet) retweet_cut,
round(sum(if_retweet) / count(1),3) retweet_rate
from tb_video_info a
left join tb_user_video_log b on a.video_id = b.video_id
WHERE DATEDIFF((SELECT MAX(end_time) FROM tb_user_video_log),start_time) <= 29
group by tag
order by retweet_rate desc
2、问题:计算2021年里每个创作者每月的涨粉率及截止当月的总粉丝量
注:
涨粉率=(加粉量 - 掉粉量) / 播放量。结果按创作者ID、总粉丝量升序排序。
if_follow-是否关注为1表示用户观看视频中关注了视频创作者,为0表示此次互动前后关注状态未发生变化,为2表示本次观看过程中取消了关注。
SELECT
A.author AS author,
A.month AS month,
ROUND(fans_situation / total_play, 3) AS fans_growth_rate,
SUM(fans_situation) OVER(PARTITION BY author ORDER BY month) AS total_fans
FROM
(SELECT
b.author AS author,
DATE_FORMAT(a.start_time,'%Y-%m') AS month,
# 粉丝变化量
SUM(
CASE
WHEN a.if_follow = 1 THEN 1
WHEN a.if_follow = 2 THEN -1
ELSE 0
END) AS fans_situation,
# 播放量
COUNT(*) AS total_play
FROM
tb_user_video_log AS a
INNER JOIN
tb_video_info AS b
ON
a.video_id = b.video_id
WHERE
YEAR(a.start_time) = 2021
AND YEAR(a.end_time) = 2021
GROUP BY
author, month) AS A
ORDER BY
author, total_fans
标签:真题,author,month,牛客,fans,video,time,tb,刷题
From: https://www.cnblogs.com/yuyilll/p/18050646