列表

详情


SQL160. 国庆期间每类视频点赞量和转发量

描述

用户-视频互动表tb_user_video_log

id uid video_id start_time end_time if_follow if_like if_retweet comment_id
1 101 2001 2021-09-24 10:00:00 2021-09-24 10:00:20
1 1 0 NULL
2 105
2002 2021-09-25 11:00:00
2021-09-25 11:00:30
0 0 1 NULL
3 102
2002 2021-09-25 11:00:00
2021-09-25 11:00:30
1 1 1 NULL
4 101
2002 2021-09-26 11:00:00
2021-09-26 11:00:30
1 0 1 NULL
5 101
2002 2021-09-27 11:00:00
2021-09-27 11:00:30
1 1 0 NULL
6 102 2002 2021-09-28 11:00:00
2021-09-28 11:00:30
1 0 1 NULL
7
103 2002 2021-09-29 11:00:00
2021-10-02 11:00:30
1 0 1 NULL
8
102 2002 2021-09-30 11:00:00
2021-09-30 11:00:30
1 1 1 NULL
9 101
2001 2021-10-01 10:00:00
2021-10-01 10:00:20
1 1 0 NULL
10 102
2001
2021-10-01 10:00:00
2021-10-01 10:00:15 0 0 1 NULL
11 103 2001
2021-10-01 11:00:50
2021-10-01 11:01:15
1 1 0 1732526
12
106 2002 2021-10-02 10:59:05
2021-10-02 11:00:05
2 0 1 NULL
13
107 2002 2021-10-02 10:59:05
2021-10-02 11:00:05
1 0 1 NULL
14
108 2002 2021-10-02 10:59:05
2021-10-02 11:00:05
1 1 1 NULL
15 109 2002 2021-10-03 10:59:05
2021-10-03 11:00:05
0 1 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 2020-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
4 2004 902 美女 90 2020-01-01 08:00:00
(video_id-视频ID, author-创作者ID, tag-类别标签, duration-视频时长, release_time-发布时间)

问题:统计2021年国庆头3天每类视频每天的近一周总点赞量和一周内最大单天转发量,结果按视频类别降序、日期升序排序。假设数据库中数据足够多,至少每个类别下国庆头3天及之前一周的每天都有播放记录。

输出示例
示例数据的输出结果如下
tag dt sum_like_cnt_7d max_retweet_cnt_7d
旅游 2021-10-01 5 2
旅游
2021-10-02 5 3
旅游
2021-10-03 6 3
解释:
由表tb_user_video_log里的数据可得只有旅游类视频的播放,2021年9月25到10月3日每天的点赞量和转发量如下:
tag
dt
like_cnt retweet_cnt
旅游
2021-09-25 1 2
旅游
2021-09-26
0 1
旅游
2021-09-27
1 0
旅游
2021-09-28
0 1
旅游
2021-09-29
0 1
旅游
2021-09-30
1 1
旅游
2021-10-01
2 1
旅游
2021-10-02
1 3
旅游
2021-10-03
1 0
因此国庆头3天(10.01~10.03)里10.01的近7天(9.25~10.01)总点赞量为5次,单天最大转发量为2次(9月25那天最大);同理可得10.02和10.03的两个指标。

示例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-09-24 10:00:00', '2021-09-24 10:00:20', 1, 1, 0, null)
  ,(105, 2002, '2021-09-25 11:00:00', '2021-09-25 11:00:30', 0, 0, 1, null)
  ,(102, 2002, '2021-09-25 11:00:00', '2021-09-25 11:00:30', 1, 1, 1, null)
  ,(101, 2002, '2021-09-26 11:00:00', '2021-09-26 11:00:30', 1, 0, 1, null)
  ,(101, 2002, '2021-09-27 11:00:00', '2021-09-27 11:00:30', 1, 1, 0, null)
  ,(102, 2002, '2021-09-28 11:00:00', '2021-09-28 11:00:30', 1, 0, 1, null)
  ,(103, 2002, '2021-09-29 11:00:00', '2021-09-29 11:00:30', 1, 0, 1, null)
  ,(102, 2002, '2021-09-30 11:00:00', '2021-09-30 11:00:30', 1, 1, 1, null)
  ,(101, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:20', 1, 1, 0, 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', 1, 1, 0, 1732526)
  ,(106, 2002, '2021-10-02 10:59:05', '2021-10-02 11:00:05', 2, 0, 1, null)
  ,(107, 2002, '2021-10-02 10:59:05', '2021-10-02 11:00:05', 1, 0, 1, null)
  ,(108, 2002, '2021-10-02 10:59:05', '2021-10-02 11:00:05', 1, 1, 1, null)
  ,(109, 2002, '2021-10-03 10:59:05', '2021-10-03 11:00:05', 0, 1, 0, null);

INSERT INTO tb_video_info(video_id, author, tag, duration, release_time) VALUES
   (2001, 901, '旅游', 30, '2020-01-01 7:00:00')
  ,(2002, 901, '旅游', 60, '2021-01-01 7:00:00')
  ,(2003, 902, '影视', 90, '2020-01-01 7:00:00')
  ,(2004, 902, '美女', 90, '2020-01-01 8:00:00');

输出:

旅游|2021-10-01|5|2
旅游|2021-10-02|5|3
旅游|2021-10-03|6|3

原站题解

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

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



select t1.tag,t1.day,sum(t2.likes),max(t2.retweet)
FROM
(select t2.tag ,left(t1.start_time,10) day,sum(t1.if_like) as likes,sum(t1.if_retweet) as retweet
from tb_user_video_log t1 left join 
tb_video_info t2
on t1.video_id=t2.video_id
group by t2.tag,day) t1
left join 
(select t2.tag ,left(t1.start_time,10) day,sum(t1.if_like) as likes,sum(t1.if_retweet) as retweet
from tb_user_video_log t1 left join 
tb_video_info t2
on t1.video_id=t2.video_id
group by t2.tag,day) t2
on t1.tag=t2.tag
where TIMESTAMPDIFF(day,t2.day,t1.day)<7
and TIMESTAMPDIFF(day,t2.day,t1.day)>=0
and t1.day in ("2021-10-01","2021-10-02","2021-10-03")
group by t1.tag,t1.day

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

with t as(
    select tag,
    date_format(start_time,'%Y-%m-%d') as dt,
    sum(if_like) as like_cnt,
    sum(if_retweet) as retweet_cnt
    from tb_user_video_log left join tb_video_info using (video_id)
    group by tag,dt
)

select *
from (
    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 t
) t1
where dt between '2021-10-01' and '2021-10-03'
order by tag desc,dt

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

SELECT * FROM
(SELECT tag, dt, 
       SUM(lick_cnt) OVER w,
       MAX(retweet_cnt) OVER w
FROM
(SELECT tag, DATE_FORMAT(start_time, '%Y-%m-%d') AS dt,
SUM(if_like) lick_cnt, SUM(if_retweet) retweet_cnt
FROM tb_user_video_log JOIN tb_video_info USING(video_id)
WHERE start_time BETWEEN '20210925' AND '20211004' GROUP BY tag, dt) t
WINDOW w AS (PARTITION BY tag ORDER BY dt DESC rows BETWEEN CURRENT ROW AND 6 FOLLOWING)
) t1
WHERE dt BETWEEN '2021-10-01' AND '2021-10-04'
ORDER BY tag DESC, dt ASC

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

# 每一类视频tag类别
#前三天分别是 01-02-03
# 近一周总点赞量if_like
# 近一周最大单日转发量if_retweet
# 视频类别降序
# j
select 
	tag, 
	dt, 
	sum(like_cnt) `sum_like_cnt_7d`, 
	max(tweet) `max_retweet_cnt_7d`
from
(select 
	tag, 
	dt,
	sum(if_like) `like_cnt`,
	sum(if_retweet) `tweet`
from 
(select vi.tag, b.dt, date(vl1.start_time) `st_date`, vl1.if_like, vl1.if_retweet
from tb_video_info vi 
left join tb_user_video_log vl1 on vi.video_id = vl1.video_id, (
	select distinct date(start_time) dt
	from tb_user_video_log vl
	where DATEDIFF(vl.start_time,'2021-10-01')>=0 and DATEDIFF(vl.start_time,'2021-10-01')<3) b
where DATEDIFF(vl1.start_time,b.dt)<=0 and DATEDIFF(vl1.start_time,b.dt)>-7) t
GROUP BY tag, dt, st_date) t1
GROUP BY tag, dt
ORDER BY tag desc, `dt`;

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

SELECT *
FROM
(SELECT 
    tag,
    dt,
    SUM(like_cnt) over (partition by tag order by dt ROWS 6 preceding) AS sum_like_cnt_7d,
    MAX(retweet_cnt) over (partition by tag order by dt rows 6 preceding) AS max_retweet_cnt_7d
FROM
(SELECT 
    tag,
    DATE(start_time) AS dt,
    SUM(IF(if_like = 1, 1, 0)) AS like_cnt,
    SUM(if(if_retweet = 1, 1, 0)) AS retweet_cnt
FROM tb_user_video_log u
LEFT JOIN tb_video_info vi
ON u.video_id = vi.video_id
GROUP BY tag,DATE(start_time)) t1
GROUP BY tag, dt) t2
WHERE dt between '2021-10-01' and '2021-10-03'
ORDER BY tag DESC,dt ASC