题解 | 每个月Top3的周杰伦歌曲
每个月Top3的周杰伦歌曲
https://www.nowcoder.com/practice/4ab6d198ea8447fe9b6a1cad1f671503
思路:
- 确认筛选条件。筛选条件是singer_name, year(fdate), age,涉及三个表,以play_log作为主表,关联后进行筛选。
- 分组聚合。需要看【每个月份】【top3】的【歌曲】,所以聚合维度是 month(fdate)和song_id。
需要注意的问题:
筛选出来的字段必须包含在group by 里。
with
tmp as (
select
month(p.fdate) as month,
s.song_id,
s.song_name,
count(*) as play_pv,
row_number() over (
partition by
month(p.fdate)
order by
count(*) desc
,s.song_id asc
) as ranking
from
play_log p
join song_info s on p.song_id = s.song_id
join user_info u on p.user_id = u.user_id
where
u.age between 18 and 25
and s.singer_name = '周杰伦'
and year(p.fdate) = 2022
group by
month(p.fdate), s.song_id, s.song_name
)
select month
, ranking
, song_name
, play_pv
from tmp
where ranking < 4