假设我们的业务场景是需要计算每个用户连续听歌最高的天数,从而方便后面去找到核心听歌用户群体,需要如何统计出每个用户的最大连续听歌天数? 播放表数据如下 其中,ftime为日期,uin为用户ID,songid为对应的歌曲ID,play_duration为听歌秒数,用户每次听一首歌会被上报至此表中 求计算出,这个表里每个用户的最大连续听歌天数
示例1

输入

DROP TABLE IF EXISTS `user_listen_record`;
CREATE TABLE `user_listen_record` (
  `ftime` bigint(20) DEFAULT NULL,
  `uin` varchar(255) DEFAULT NULL,
  `os_type` varchar(255) DEFAULT NULL,
  `song_id` bigint(20) DEFAULT NULL,
  `app_ver` varchar(255) DEFAULT NULL,
  `play_duration` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of user_listen_record
-- ----------------------------
BEGIN;
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'a', 'ios', 1001, '10.0.1', 140);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 's', 'android', 1001, '10.0.1', 170);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'm', 'ios', 1003, '10.0.5', 100);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'u', 'android', 1004, '10.0.1', 229);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'm', 'ios', 1002, '10.0.5', 230);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'a', 'ios', 1003, '10.0.1', 257);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'u', 'android', 1001, '10.0.1', 290);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 's', 'android', 1003, '10.0.1', 170);
INSERT INTO `user_listen_record` (`ftime`, `uin`, `os_type`, `song_id`, `app_ver`, `play_duration`) VALUES (20230306, 'a', 'ios', 1004, '10.0.1', 229);
COMMIT;

输出

uin|max_continuation_date_cnt
a|1
m|1
s|1
u|1

备注:
最大连续听歌天数需要两次听歌日期相邻,直接统计每个用户的听歌天数无法完成本题目
加载中...