列表

详情


SQL159. 每个创作者每月的涨粉率及截止当前的总粉丝量

描述

用户-视频互动表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-01 10:00:00 2021-09-01 10:00:20
0 1 1 NULL
2 105
2002 2021-09-10 11:00:00
2021-09-10 11:00:30
1 0 1 NULL
3 101
2001 2021-10-01 10:00:00
2021-10-01 10:00:20
1 1 1 NULL
4 102
2001 2021-10-01 10:00:00
2021-10-01 10:00:15
0 0 1 NULL
5 103
2001 2021-10-01 11:00:50
2021-10-01 11:01:15
1 1 0 1732526
6 106 2002 2021-10-01 10:59:05
2021-10-01 11:00:05
2 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
4 2004 902 美女 90 2020-01-01 08:00:00
(video_id-视频ID, author-创作者ID, tag-类别标签, duration-视频时长, release_time-发布时间)

问题:计算2021年里每个创作者每月的涨粉率及截止当月的总粉丝量

  • 涨粉率=(加粉量 - 掉粉量) / 播放量。结果按创作者ID、总粉丝量升序排序。
  • if_follow-是否关注为1表示用户观看视频中关注了视频创作者,为0表示此次互动前后关注状态未发生变化,为2表示本次观看过程中取消了关注。

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

author month fans_growth_rate
total_fans
901 2021-09 0.500 1
901 2021-10
0.250 2

解释:
示例数据中表tb_user_video_log里只有视频2001和2002的播放记录,都来自创作者901,播放时间在2021年9月和10月;其中9月里加粉量为1,掉粉量为0,播放量为2,因此涨粉率为0.500(保留3位小数);其中10月里加粉量为2,掉份量为1,播放量为4,因此涨粉率为0.250,截止当前总粉丝数为2。

示例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-01 10:00:00', '2021-09-01 10:00:20', 0, 1, 1, null)
  ,(105, 2002, '2021-09-10 11:00:00', '2021-09-10 11:00:30', 1, 0, 1, null)
  ,(101, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:20', 1, 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', 1, 1, 0, 1732526)
  ,(106, 2002, '2021-10-01 10:59:05', '2021-10-01 11:00:05', 2, 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')
  ,(2004, 902, '美女', 90, '2020-01-01 8:00:00');

输出:

901|2021-09|0.500|1
901|2021-10|0.250|2

原站题解

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

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

SELECT B.AUTHOR AS AUTHOR,DATE_FORMAT(A.start_time,'%Y-%m') AS MONTH ,
ROUND((COUNT(CASE WHEN A.if_follow=1 THEN 1 END ) - COUNT(CASE WHEN A.if_follow=2 THEN 1 END ))/COUNT(1) ,3) AS FANS_GROWTH_RATE,
sum(sum(case when A.if_follow = 1 then 1
         when A.if_follow = 2 then -1
         else 0 end) ) over (partition by B.author order by date_format(A.start_time,'%Y-%m')) fans_total
FROM tb_user_video_log A 
LEFT JOIN tb_video_info B 
ON A.VIDEO_ID=B.video_id
WHERE year(A.start_time)=2021
and year(A.end_time)=2021
GROUP BY B.AUTHOR,DATE_FORMAT(A.start_time,'%Y-%m')
ORDER BY AUTHOR,fans_total

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

SELECT 
  author, 
  DATE_FORMAT(start_time, '%Y-%m') month, 
  ROUND(SUM(IF(if_follow = 2, -1, if_follow)) / COUNT(*), 3) fans_growth_rate, 
  SUM(SUM(IF(if_follow = 2, -1, if_follow))) OVER (PARTITION BY author ORDER BY DATE_FORMAT(start_time, '%Y-%m')) total_fans 
FROM tb_video_info 
LEFT JOIN tb_user_video_log USING(video_id) 
WHERE YEAR(start_time) = 2021
GROUP BY 1,2
ORDER BY 1,4

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

SELECT t.author, t.month, 
    ROUND(t.add_fans / t.all_watch, 3) `fans_growth_rate`,
    SUM(t.add_fans) OVER(PARTITION BY t.author ORDER BY t.month) `total_fans`
FROM (
    SELECT  author, left(u.start_time, 7) `month`, 
        (SUM(IF(u.if_follow = 1, 1, 0)) - SUM(IF(u.if_follow = 2, 1, 0))) `add_fans`,
        COUNT(*) `all_watch`
        FROM tb_video_info v 
        INNER JOIN tb_user_video_log u
        ON v.video_id = u.video_id
        WHERE DATEDIFF(u.start_time, '2021-01-01 00:00:00') >= 0
        GROUP BY author, left(u.start_time, 7)
)t
ORDER BY author, total_fans

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

SELECT author, month,
       ROUND((increase-decrease)/watch, 3) AS fans_growth_rate,
       SUM(increase-decrease) OVER(PARTITION BY author ORDER BY month) AS total_fans
FROM (
    SELECT i.author, DATE_FORMAT(end_time, "%Y-%m") AS month,
           SUM(IF(l.if_follow=1, 1, 0)) AS increase,
           SUM(IF(l.if_follow=2, 1, 0)) AS decrease,
           COUNT(uid) AS watch
    FROM tb_user_video_log l
    JOIN tb_video_info i
    ON l.video_id = i.video_id
    WHERE YEAR(end_time) = 2021
    GROUP BY i.author, month
) AS a
ORDER BY author, total_fans

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

  SELECT author,DATE_FORMAT(start_time,'%Y-%m')MONTH,
  ROUND(SUM(CASE if_follow WHEN 1 THEN 1 WHEN 0 THEN 0 ELSE -1 END)/COUNT(start_time),3) fans_growth_rate,
 sum(SUM(CASE if_follow WHEN 1 THEN 1 WHEN 0 THEN 0 ELSE -1 END)) over (partition by author order by DATE_FORMAT(start_time,'%Y-%m')) total_fans
  FROM `tb_user_video_log` l LEFT JOIN `tb_video_info` info 
  ON l.`video_id`=info.`video_id`
  where year(start_time)=2021 and year(end_time)=2021
  GROUP BY author,MONTH
  ORDER BY author,total_fans