题解 | #每个月Top3的周杰伦歌曲#

每个月Top3的周杰伦歌曲

https://www.nowcoder.com/practice/4ab6d198ea8447fe9b6a1cad1f671503

select * from (
select d.month,
row_number() over(partition by d.month order by d.play_pv desc,d.song_id) as ranking ,d.song_name,d.play_pv
from
(
select   
month(a.fdate)  month, 
 c.song_name,c.song_id,count(*)play_pv

from play_log a
left join user_info b on a.user_id=b.user_id
left join song_info c on a.song_id=c.song_id

where b.age>=18 and b.age<=25 and year(a.fdate)=2022 and c.singer_name='周杰伦'
group by month,song_name,song_id
order by month ,play_pv desc,song_id) d
) e
where e.ranking  <=3


本题有以下难点和易错点

1 row_number()的使用 :

语法:row_number() over(partition by d.month order by d.play_pv desc,d.song_id)

PARTITION BY子句将结果集划分为分区。 ROW_NUMBER()函数分别应用于每个分区,并重新初始化每个分区的行号。PARTITION BY子句是可选的。如果未指定,ROW_NUMBER()函数会将整个结果集视为单个分区。ORDER BY子句定义结果集的每个分区中的行的逻辑顺序。 ORDER BY子句是必需的,因为ROW_NUMBER()函数对顺序敏感。

2 where 后不可以直接使用ranking<=3 where后只能查询从数据库查询出来的数据,必须要用select再包裹一层

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务