列表

详情


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
uid-用户ID, video_id-视频ID, start_time-开始观看时间, end_time-结束观看时间, if_follow-是否关注, if_like-是否点赞, if_retweet-是否转发, comment_id-评论ID)


短视频信息表tb_video_info
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


(video_id-视频ID, author-创作者ID, tag-类别标签, duration-视频时长, release_time-发布时间)


问题:统计在有用户互动的最近一个月(按包含当天在内的近30天算,比如10月31日的近30天为10.2~10.31之间的数据)中,每类视频的转发量和转发率(保留3位小数)。

:转发率=转发量÷播放量。结果按转发率降序排序。

输出示例
示例数据的输出结果如下

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 36ms, 内存消耗: 6392KB, 提交时间: 2022-01-01

# select tag
# ,concat(round(avg(jd)*100,2),"%") as avg_play_progres
# FROM(select tuvl.id
# ,if(TIMESTAMPDIFF(second, start_time, end_time)/duration>1,1,TIMESTAMPDIFF(second, start_time, end_time)/duration) as jd
# ,tag
# from tb_user_video_log as tuvl left join tb_video_info as tvi on tuvl.video_id=tvi.video_id) as t
# group by tag
# having avg(jd)>0.6
# order by avg(jd) desc



select tag
,sum(if_retweet) as retweet_cnt
,round(sum(if_retweet)/count(*),3) as retweet_rate
FROM(select tuvl.id
,if_retweet
,start_time
,end_time
,tag
from tb_user_video_log as tuvl left join tb_video_info as tvi on tuvl.video_id=tvi.video_id
) as t
where datediff((select max(start_time) FROM tb_user_video_log),start_time)<=29
group by tag
order by retweet_rate desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6400KB, 提交时间: 2021-12-14

select b.tag, sum(if_retweet) retweet_cnt,  round(sum(if_retweet)/count(1),3) retweet_rate from
tb_user_video_log a
left join  tb_video_info b
on a.video_id = b.video_id
where DATEDIFF(DATE((select max(start_time) from tb_user_video_log)) ,DATE(start_time)) <= 29
group by b.tag
order by 2 desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6404KB, 提交时间: 2022-01-25

# select tag,sum(if_retweet),round(sum(if_retweet)/count(*),3) as r from tb_user_video_log
# join tb_video_info ON
# tb_user_video_log.video_id=tb_video_info.video_id
# WHERE DATEDIFF(DATE((select max(start_time) FROM tb_user_video_log)), DATE(tb_user_video_log.start_time)) <= 29
# group by tag
# order by r desc

select tag,sum(if_retweet),round(sum(if_retweet)/count(*),3) as r from tb_user_video_log
join tb_video_info ON tb_user_video_log.video_id=tb_video_info.video_id
WHERE DATEDIFF(DATE((select max(start_time) FROM tb_user_video_log)), DATE(tb_user_video_log.start_time)) <= 29
group by tag
order by r desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6416KB, 提交时间: 2022-01-27

SELECT b.tag, sum(a.if_retweet) retweet_cnt, round(avg(a.if_retweet),3) retweet_rate
FROM
tb_user_video_log a
join tb_video_info b
on a.video_id = b.video_id
where a.start_time >= 
(SELECT DATE_SUB(max(start_time), INTERVAL 30 DAY)
FROM tb_user_video_log)
group by b.tag
order by retweet_rate desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6420KB, 提交时间: 2021-12-14

select tag,
sum(if_retweet) as retweet_count,
round(sum(if_retweet) / count(*), 3) as retweet_rate
from tb_user_video_log t1
join tb_video_info t2
on t1.video_id = t2.video_id
where datediff(date((select max(start_time) from tb_user_video_log)), date(start_time)) <= 29
group by tag
order by retweet_rate desc