-
Notifications
You must be signed in to change notification settings - Fork 0
/
Top5Artists.sql
54 lines (51 loc) · 926 Bytes
/
Top5Artists.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
WITH
-- 'song appearances' in the daily chart (top 10 position only)
-- row = song
song_appearances AS (
SELECT
song_id,
COUNT(*) as appearances
FROM
global_song_rank
WHERE
rank <= 10
GROUP BY
song_id
),
-- number of 'song apperances' per artists
-- row = artist
artist_appearances AS (
SELECT
artist_id,
SUM(song_appearances.appearances) as appearances
FROM
song_appearances
LEFT JOIN
songs
ON song_appearances.song_id = songs.song_id
GROUP BY
artist_id
),
-- rank (according to song appearances) of artists
-- row = artist
artist_ranks AS (
SELECT
artists.artist_name,
DENSE_RANK() OVER(
ORDER BY appearances DESC
) as artist_rank
FROM
artist_appearances
LEFT JOIN
artists
ON artist_appearances.artist_id = artists.artist_id
)
SELECT
*
FROM
artist_ranks
WHERE
artist_rank <= 5
ORDER BY
artist_rank, artist_name
;