SQL158 每类视频近一个月的转发量/率
2022/9/13 2:24:20
本文主要是介绍SQL158 每类视频近一个月的转发量/率,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
描述
用户-视频互动表tb_user_video_log
id | uid | video_id | start_time | end_time | if_follow | if_like | if_retweet | comment_id |
1 | 101 | 2001 | 2021-10-01 10:00:00 | 2021-10-01 10:00:20 | 0 | 1 | 1 | NULL |
2 | 102 | 2001 | 2021-10-01 10:00:00 | 2021-10-01 10:00:15 | 0 | 0 | 1 | NULL |
3 | 103 | 2001 | 2021-10-01 11:00:50 | 2021-10-01 11:01:15 | 0 | 1 | 0 | 1732526 |
4 | 102 | 2002 | 2021-09-10 11:00:00 | 2021-09-10 11:00:30 | 1 | 0 | 1 | NULL |
5 | 103 | 2002 | 2021-10-01 10:59:05 | 2021-10-01 11:00:05 | 1 | 0 | 0 | NULL |
id | video_id | author | tag | duration | release_time |
1 | 2001 | 901 | 影视 | 30 | 2021-01-01 07:00:00 |
2 | 2002 | 901 | 美食 | 60 | 2021-01-01 07:00:00 |
3 | 2003 | 902 | 旅游 | 90 | 2020-01-01 07:00:00 |
tag | retweet_cut | retweet_rate |
影视 | 2 | 0.667 |
美食 | 1 | 0.500 |
解释: 由表tb_user_video_log的数据可得,数据转储当天为2021年10月1日。近30天内,影视类视频2001共有3次播放记录,被转发2次,转发率为0.667;美食类视频2002共有2次播放记录,1次被转发,转发率为0.500。
示例1
输入:DROP TABLE IF EXISTS tb_user_video_log, tb_video_info; CREATE TABLE tb_user_video_log ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid INT NOT NULL COMMENT '用户ID', video_id INT NOT NULL COMMENT '视频ID', start_time datetime COMMENT '开始观看时间', end_time datetime COMMENT '结束观看时间', if_follow TINYINT COMMENT '是否关注', if_like TINYINT COMMENT '是否点赞', if_retweet TINYINT COMMENT '是否转发', comment_id INT COMMENT '评论ID' ) CHARACTER SET utf8 COLLATE utf8_bin; CREATE TABLE tb_video_info ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', video_id INT UNIQUE NOT NULL COMMENT '视频ID', author INT NOT NULL COMMENT '创作者ID', tag VARCHAR(16) NOT NULL COMMENT '类别标签', duration INT NOT NULL COMMENT '视频时长(秒数)', release_time datetime NOT NULL COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_bin; INSERT INTO tb_user_video_log(uid, video_id, start_time, end_time, if_follow, if_like, if_retweet, comment_id) VALUES (101, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:20', 0, 1, 1, null) ,(102, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:15', 0, 0, 1, null) ,(103, 2001, '2021-10-01 11:00:50', '2021-10-01 11:01:15', 0, 1, 0, 1732526) ,(102, 2002, '2021-09-10 11:00:00', '2021-09-10 11:00:30', 1, 0, 1, null) ,(103, 2002, '2021-10-01 10:59:05', '2021-10-01 11:00:05', 1, 0, 0, null); INSERT INTO tb_video_info(video_id, author, tag, duration, release_time) VALUES (2001, 901, '影视', 30, '2021-01-01 7:00:00') ,(2002, 901, '美食', 60, '2021-01-01 7:00:00') ,(2003, 902, '旅游', 90, '2020-01-01 7:00:00');输出:
影视|2|0.667 美食|1|0.500
步骤一:先算出转发率
select round(avg(tb1.if_retweet),3) as retweet_rate from tb_user_video_log tb1
步骤二:根据类别算出每种视频的转发率
select tb2.tag tag, sum(tb1.if_retweet) retweet_cut, round(avg(tb1.if_retweet),3) as retweet_rate from tb_user_video_log tb1 left join tb_video_info tb2 on tb1.video_id = tb2.video_id group by tb2.tag
步骤三:根据时间找到近一个月数据和排序
select tb2.tag tag, sum(tb1.if_retweet) retweet_cut, round(avg(tb1.if_retweet),3) as retweet_rate from tb_user_video_log tb1 left join tb_video_info tb2 on tb1.video_id = tb2.video_id WHERE DATEDIFF(DATE((select max(start_time) FROM tb_user_video_log)) , DATE(tb1.start_time)) <= 29 group by tb2.tag ORDER BY retweet_rate desc
这篇关于SQL158 每类视频近一个月的转发量/率的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22[开源]10.3K+ Star!轻量强大的开源运维平台,超赞!
- 2024-11-21Flutter基础教程:新手入门指南
- 2024-11-21Flutter跨平台教程:新手入门详解
- 2024-11-21Flutter跨平台教程:新手入门与实践指南
- 2024-11-21Flutter列表组件教程:初学者指南
- 2024-11-21Flutter列表组件教程:新手入门指南
- 2024-11-21Flutter入门教程:初学者必看指南
- 2024-11-21Flutter入门教程:从零开始的Flutter开发指南
- 2024-11-21Flutter升级教程:新手必读的升级指南
- 2024-11-21Flutter升级教程:轻松掌握Flutter版本更新