# 需求分析:时间18-25岁,2022年,周杰伦,排序song_id
# 实现逻辑:
# 1、多表关联
# 2、play_pv 播放次数,不去重
# 3、排名计算,按play_pv降序排名,row_number(),song_id的内容
# 函数注意:
# 月度:month(fdate)对应数字
# row_number能嵌套聚合函数
# 不能缺少条件,周杰伦
# 不能缺少对应内容
# 来源表要识别清晰
# 关键词要引用
# group by条件
# 分组的时候使用时间分组
with temp as
(
SELECT month(a.fdate) as `month`,
row_number() over(partition by month(a.fdate) order by COUNT(b.song_name) desc,a.song_id asc) as ranking,
b.song_name,
COUNT(b.song_name) as play_pv
FROM play_log a
left join song_info b
on a.song_id=b.song_id
left join user_info c
on a.user_id=c.user_id
where c.age between 18 and 25
and year(a.fdate)=2022
and b.singer_name='周杰伦'
group by month(a.fdate),b.song_name,a.song_id
)
SELECT *
from temp
where ranking<=3