Description
Table: spotify_history
+------------+---------+ | Column Name| Type | +------------+---------+ | history_id | integer | | user_id | integer | | song_id | integer | | song_plays | integer | +------------+---------+
Table: spotify_streaming
+-------------+----------+ | Column Name | Type | +-------------+----------+ | user_id | integer | | song_id | integer | | listen_time | datetime | +-------------+----------+
spotify_historystores pre aggregated play counts through July 31st.spotify_streamingstores individual listen events for the current week (August 1 through 7).
Write a query that outputs each user's cumulative song play counts through August 4th, 2022, combining historical data from spotify_history with current week events from spotify_streaming (filtered to on or before August 4th). Return user_id, song_id, and song_plays, sorted by song_plays descending.
The result format is shown below.
Example
Input:
spotify_history table: +------------+---------+---------+------------+ | history_id | user_id | song_id | song_plays | +------------+---------+---------+------------+ | 10011 | 777 | 1238 | 11 | | 12452 | 695 | 4520 | 1 | +------------+---------+---------+------------+ spotify_streaming table: +---------+---------+---------------------+ | user_id | song_id | listen_time | +---------+---------+---------------------+ | 777 | 1238 | 08/01/2022 12:00:00 | | 695 | 4520 | 08/04/2022 08:00:00 | | 125 | 9630 | 08/04/2022 16:00:00 | | 695 | 9852 | 08/07/2022 12:00:00 | +---------+---------+---------------------+Output:
+---------+---------+------------+ | user_id | song_id | song_plays | +---------+---------+------------+ | 777 | 1238 | 12 | | 695 | 4520 | 2 | | 125 | 9630 | 1 | +---------+---------+------------+Explanation:
- User 777 had 11 historical plays of song 1238 plus 1 stream on Aug 1 = 12.
- User 695 had 1 historical play of song 4520 plus 1 stream on Aug 4 = 2; their Aug 7 stream for song 9852 is excluded (after cutoff).
- User 125 had no history for song 9630 but 1 stream on Aug 4 = 1.
- User 695's song 9852 row drops entirely because it has no history and its only stream is after Aug 4.
Accepted
9/17
Acceptance Rate52.9%
Ask AI Assistant

Ask AI Assistant
Ask me anything about this coding question!
Test Cases
Input
-
Output
-
Expected
-