-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_project_sql_statements.py
47 lines (43 loc) · 1.59 KB
/
final_project_sql_statements.py
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
class SqlQueries:
songplay_table_insert = ("""
SELECT
md5(events.sessionid || events.start_time) songplay_id,
events.start_time,
events.userid,
events.level,
songs.song_id,
songs.artist_id,
events.sessionid,
events.location,
events.useragent
FROM (SELECT TIMESTAMP 'epoch' + ts/1000 * interval '1 second' AS start_time, *
FROM staging_events
WHERE page='NextSong') events
LEFT JOIN staging_songs songs
ON events.song = songs.title
AND events.artist = songs.artist_name
AND events.length = songs.duration
""")
user_table_insert = ("""
SELECT distinct userid, firstname, lastname, gender, level
FROM staging_events
WHERE page='NextSong'
""")
song_table_insert = ("""
SELECT distinct song_id, title, artist_id, year, duration
FROM staging_songs
""")
artist_table_insert = ("""
SELECT distinct artist_id, artist_name,
artist_location, artist_latitude,
artist_longitude
FROM staging_songs
""")
time_table_insert = ("""
SELECT
start_time, extract(hour from start_time),
extract(day from start_time), extract(week from start_time),
extract(month from start_time), extract(year from start_time),
extract(dayofweek from start_time)
FROM songplays
""")