首页 > 试题广场 >

每个月Top3的周杰伦歌曲

[编程题]每个月Top3的周杰伦歌曲
  • 热度指数:120017 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
从听歌流水中找到18-25岁用户在2022年每个月播放次数top 3的周杰伦的歌曲。

流水表 play_log:
日期 (fdate) 用户 ID (user_id) 歌曲 ID (song_id)
2022-01-08 10000 0
2022-01-16 10000 0
2022-01-20 10000 0
2022-01-25 10000 0
2022-01-02 10000 1
2022-01-12 10000 1
2022-01-13 10000 1
2022-01-14 10000 1
2022-01-10 10000 2
2022-01-11 10000 3
2022-01-16 10000 3
2022-01-11 10000 4
2022-01-27 10000 4
2022-02-05 10000 0
2022-02-19 10000 0
2022-02-07 10000 1
2022-02-27 10000 2
2022-02-25 10000 3
2022-02-03 10000 4
2022-02-16 10000 4
歌曲表song_info
歌曲 ID (song_id) 歌曲名称 (song_name) 歌手名称 (singer_name)
0 明明就 周杰伦
1 说好的幸福呢 周杰伦
2 江南 林俊杰
3 大笨钟 周杰伦
4 黑键 林俊杰
用户表user_info
user_id age
10000 18
输出:
month ranking song_name play_pv
1 1 明明就 4
1 2 说好的幸福呢 4
1 3 大笨钟 2
2 1 明明就 2
2 2 说好的幸福呢 1
2 3 大笨钟 1

示例1

输入

drop table if exists play_log;
create table `play_log` (
    `fdate` date,
    `user_id` int,
    `song_id` int
);
insert into play_log(fdate, user_id, song_id)
values 
('2022-01-08', 10000, 0),
('2022-01-16', 10000, 0),
('2022-01-20', 10000, 0),
('2022-01-25', 10000, 0),
('2022-01-02', 10000, 1),
('2022-01-12', 10000, 1),
('2022-01-13', 10000, 1),
('2022-01-14', 10000, 1),
('2022-01-10', 10000, 2),
('2022-01-11', 10000, 3),
('2022-01-16', 10000, 3),
('2022-01-11', 10000, 4),
('2022-01-27', 10000, 4),
('2022-02-05', 10000, 0),
('2022-02-19', 10000, 0),
('2022-02-07', 10000, 1),
('2022-02-27', 10000, 2),
('2022-02-25', 10000, 3),
('2022-02-03', 10000, 4),
('2022-02-16', 10000, 4);

drop table if exists song_info;
create table `song_info` (
    `song_id` int,
    `song_name` varchar(255),
    `singer_name` varchar(255)
);
insert into song_info(song_id, song_name, singer_name) 
values
(0, '明明就', '周杰伦'),
(1, '说好的幸福呢', '周杰伦'),
(2, '江南', '林俊杰'),
(3, '大笨钟', '周杰伦'),
(4, '黑键', '林俊杰');

drop table if exists user_info;
create table `user_info` (
    `user_id`   int,
    `age`       int
);
insert into user_info(user_id, age) 
values
(10000, 18)

输出

month|ranking|song_name|play_pv
1|1|明明就|4
1|2|说好的幸福呢|4
1|3|大笨钟|2
2|1|明明就|2
2|2|说好的幸福呢|1
2|3|大笨钟|1

说明

1月被18-25岁用户播放次数最高的三首歌为“明明就”、“说好的幸福呢”、“大笨钟”,“明明就”和“说好的幸福呢”播放次数相同,排名先后由两者的song_id先后顺序决定。2月同理。



备注:
MySQL中,日期转月份的函数为 month(),例:SELECT MONTH(‘2016-01-16') 返回 1。
头像 牛客题解官
发表于 2025-02-27 10:58:14
精华题解 这道题目要求我们从听歌流水中找出18-25岁用户在2022年每个月播放次数最多的前三首周杰伦的歌曲。下面是这个SQL查询的思路和实现步骤。 题目描述 我们需要从三个表中提取数据: play_log:记录了用户的听歌流水,包括日期、用户ID和歌曲ID。 song_info:记录了歌曲的信息,包括歌曲 展开全文
头像 liudelantu
发表于 2024-08-23 16:35:33
-- 问题提炼:18-25岁用户 在2022年 周杰伦的歌曲 每个月播放次数top 3 -- ORDER BY COUNT(s.song_name) DESC: -- 对当前分区(即当前月份)内的每首歌曲的播放记录数量进行统计,播放次数多的歌曲会排在前面 WITH t AS ( S 展开全文
头像 zy2288
发表于 2024-08-08 18:06:29
select month,ranking,song_name,play_pv from ( select month, row_number() over(partition by month order by play_pv desc,song_id) as ranking, 展开全文
头像 孤独的托尼给你点了个赞
发表于 2024-09-05 23:50:25
select * from ( select month(fdate) month, row_number()over(partition by month(fdate) order by count(song_name) desc,s.song_id asc) ranking, son 展开全文
头像 莫尔123
发表于 2024-08-22 12:32:15
select month,ranking,song_name,play_pv from (select pl_month month,row_number() over (partition by pl_month order by cnt DESC,song_id) ranking,so 展开全文
头像 牛客726352409号
发表于 2024-08-13 16:23:11
WITH a AS ( SELECT month(fdate) AS month, t1.user_id, t1.song_id, t2.song_name 展开全文
头像 imprevu
发表于 2025-02-24 15:34:41
题目描述 本题要求从听歌流水数据中筛选出18-25岁用户在2022年每个月播放次数排名前3的周杰伦歌曲。涉及三张表: 流水表play_log:记录了日期(fdate)、用户ID(user_id)、歌曲ID(song_id)等信息。 歌曲表song_info:包含歌曲ID(song_id)、歌曲名称 展开全文
头像 KoolHero
发表于 2024-08-11 23:26:43
with tmp as ( select month(t1.fdate) as month , row_number() over(partition by month(t1.fdate) order by count(t1.song_id) desc, t1.song_i 展开全文
头像 牛客356472052号
发表于 2024-10-11 16:51:47
--合并三个表 --筛选条件:18-25岁,2022年,周杰伦的歌曲 --分组标准:GROUP BY 每个月,每首歌 --计算每首歌的播放次数:count(*) as play_pv --使用窗口函数 ROW_NUMBER()/DENSCE_RANK()增加ranking这一列, 注意当 p 展开全文
头像 已run的羚羊很纯真
发表于 2024-10-21 16:08:06
select * from (select month(p.fdate) `month`, row_number()over(partition by month(fdate) order by count(p.user_id) desc, p.song_id asc) ranking, song_ 展开全文
头像 Juicelabxx
发表于 2024-08-26 06:03:39
with ranked as ( select month, row_number() over (partition by month order by play_pv DESC, song_id ASC) as ranking, song_name, play_ 展开全文