-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlqueries.py
More file actions
144 lines (138 loc) · 3.85 KB
/
Copy pathsqlqueries.py
File metadata and controls
144 lines (138 loc) · 3.85 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# finds top 5 most listened to tracks
"""
SELECT trackName, artistName, SUM(msPlayed) / 60000 AS totalMinutes
FROM streaming_history
GROUP BY trackName, artistName
HAVING trackName IS NOT NULL
ORDER BY totalMinutes DESC
LIMIT 5;
"""
# finds total listening time
"""
SELECT SUM(msPlayed) / 3600000 AS totalHours
FROM streaming_history;
"""
# finds top 5 most played artists
"""
SELECT artistName, SUM(msPlayed) / 60000 AS totalMinutes
FROM streaming_history
GROUP BY artistName
HAVING artistName IS NOT NULL
ORDER BY totalMinutes DESC
limit 5;
"""
# finds top 5 most played songs from your top played artist
"""
SELECT trackName, SUM(msPlayed) / 60000 AS totalMinutes
FROM streaming_history
WHERE artistName = (SELECT artistName
FROM streaming_history
GROUP BY artistName
ORDER BY SUM(msPlayed) DESC LIMIT 1)
GROUP BY trackName
ORDER BY totalMinutes DESC
limit 5;
"""
# finds most played songs during summer months (05, 06, 07, 08) (im including may bc its usually when i start to feel happier in my music LOl)
"""
WITH MonthlyPlayCounts AS (
-- filter for summer months, year >= 2020, exclude skipped and null artist, and count plays
SELECT
EXTRACT(MONTH FROM endtime) AS summer_month,
artistname,
trackname,
COUNT(*) AS play_count
FROM
streaming_history
WHERE
EXTRACT(MONTH FROM endtime) IN (5, 6, 7, 8)
AND endtime >= '2020-01-01'
AND skipped = false
AND artistname IS NOT null
GROUP BY
EXTRACT(MONTH FROM endtime),
artistname,
trackname
),
RankedSongs AS (
-- rank the songs within each month based on play count
SELECT
summer_month,
artistname,
trackname,
play_count,
DENSE_RANK() OVER (PARTITION BY summer_month ORDER BY play_count DESC) AS rank
FROM
MonthlyPlayCounts
)
-- filter for only the top 10 songs per month
SELECT
summer_month,
rank,
artistname,
trackname,
play_count
FROM
RankedSongs
WHERE
rank <= 10
ORDER BY
summer_month,
rank;
"""
# finds the most played songs in the winter months
"""
WITH MonthlyPlayCounts AS (
-- filter for winter months, year >= 2020, exclude skipped and null artist, and count plays
SELECT
EXTRACT(MONTH FROM endtime) AS winter_month,
artistname,
trackname,
COUNT(*) AS play_count
FROM
streaming_history
WHERE
EXTRACT(MONTH FROM endtime) IN (11, 12, 1, 2)
AND endtime >= '2020-01-01'
AND skipped = false
AND artistname IS NOT null
GROUP BY
EXTRACT(MONTH FROM endtime),
artistname,
trackname
),
RankedSongs AS (
-- rank the songs within each month based on play count
SELECT
winter_month,
artistname,
trackname,
play_count,
DENSE_RANK() OVER (PARTITION BY winter_month ORDER BY play_count DESC) AS rank
FROM
MonthlyPlayCounts
)
-- filter for only the top 10 songs per month
SELECT
winter_month,
rank,
artistname,
trackname,
play_count
FROM
RankedSongs
WHERE
rank <= 10
ORDER BY
winter_month,
rank;
"""
# finds my top 5 taylor swift songs
"""
SELECT trackName, SUM(msPlayed) / 60000 AS totalMinutes
FROM streaming_history
WHERE artistName LIKE '%Taylor Swift%'
GROUP BY trackName
ORDER BY totalMinutes DESC
LIMIT 5;
"""