题解 | #每类视频近一个月的转发量/率#
每类视频近一个月的转发量/率
https://www.nowcoder.com/practice/a78cf92c11e0421abf93762d25c3bfad
# 问题:统计在有用户互动的最近一个月(按包含当天在内的近30天算,比如10月31日的近30天为10.2~10.31之间的数据)中,每类视频的转发量和转发率(保留3位小数)。
# 注:转发率=转发量÷播放量。结果按转发率降序排序。
普通解法:
转发量:if_retweet=1求和
最近时间:max(start_time)
select tag ,sum(if(if_retweet=1,1,0)) zhuanfaliang ,round(sum(if(if_retweet=1,1,0)) / count(if_retweet),3) zhuangfalv from tb_user_video_log a join tb_video_info b on a.video_id=b.video_id where timestampdiff(day,start_time, (select max(start_time) from tb_user_video_log ))<30 group by tag order by zhuangfalv desc
转发量:if_retweet=1求和
转发率:if_retweet的平均值
limit 1 也可以求出最近时间
select tag ,sum(if(if_retweet=1,1,0)) zhuanfaliang ,round(avg(if_retweet),3) zhuangfalv from tb_user_video_log a join tb_video_info b on a.video_id=b.video_id where timestampdiff(day,start_time, (select start_time from tb_user_video_log limit 1))<30 group by tag order by zhuangfalv desc