def get_streaming_data_sql(current_date_str, yesterday_str, seven_days_ago_str, tracks_tuple): sq = """select CONCAT(isrc, '_', country_code) as ISRC_GEO, sum(case when download_activity_date = '"""+current_date_str+"""' then STREAMS else 0 end) as total_streams, sum(case when download_activity_date = '"""+current_date_str+"""' then SKIPS else 0 end) as total_skips, sum(case when download_activity_date = '"""+current_date_str+"""' then SAVES else 0 end) as total_saves, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_PASSIVE else 0 end) as total_streams_passive, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_ACTIVE else 0 end) as total_streams_active, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_COLLECTION else 0 end) as total_collection_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then SUB_TYPE_SUBSCRIPTION else 0 end) as total_paid_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then SUB_TYPE_ADSUPPORTED else 0 end) as total_free_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_COLLECTION else 0 end) as spotify_collection_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_PLAYQUEUE else 0 end) as spotify_playqueue_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_ALBUMPAGE else 0 end) as spotify_album_page_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_ARTISTPAGE else 0 end) as spotify_artist_page_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_SEARCH else 0 end) as spotify_search_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_PLAYLISTS else 0 end) as spotify_playlist_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_RELEASERADAR else 0 end) as spotify_release_radar_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_DISCOVERWEEKLY else 0 end) as spotify_discover_weekly_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_RADIO else 0 end) as spotify_radio_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_DAILYMIX else 0 end) as spotify_dailymix_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_CHART else 0 end) as spotify_chart_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_SPOTIFY_OTHER else 0 end) as spotify_other_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_LIBRARY else 0 end) as apple_library_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_EXTERNAL else 0 end) as apple_external_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_VOICE else 0 end) as apple_voice_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_SEARCH else 0 end) as apple_search_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_MUSICKIT else 0 end) as apple_musickit_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_NOWPLAYING else 0 end) as apple_nowplaying_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_DISCOVERY else 0 end) as apple_discovery_streams, SUM(case when download_activity_date = '"""+current_date_str+"""' then STREAMS_SOS_APPLE_OTHER else 0 end) as apple_other_streams, sum(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS else 0 end) as yest_total_streams, sum(case when download_activity_date = '"""+yesterday_str+"""' then SKIPS else 0 end) as yest_total_skips, sum(case when download_activity_date = '"""+yesterday_str+"""' then SAVES else 0 end) as yest_total_saves, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_PASSIVE else 0 end) as yest_total_streams_passive, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_ACTIVE else 0 end) as yest_total_streams_active, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_COLLECTION else 0 end) as yest_total_collection_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then SUB_TYPE_SUBSCRIPTION else 0 end) as yest_total_paid_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then SUB_TYPE_ADSUPPORTED else 0 end) as yest_total_free_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_COLLECTION else 0 end) as yest_spotify_collection_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_PLAYQUEUE else 0 end) as yest_spotify_playqueue_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_ALBUMPAGE else 0 end) as yest_spotify_album_page_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_ARTISTPAGE else 0 end) as yest_spotify_artist_page_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_SEARCH else 0 end) as yest_spotify_search_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_PLAYLISTS else 0 end) as yest_spotify_playlist_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_RELEASERADAR else 0 end) as yest_spotify_release_radar_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_DISCOVERWEEKLY else 0 end) as yest_spotify_discover_weekly_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_RADIO else 0 end) as yest_spotify_radio_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_DAILYMIX else 0 end) as yest_spotify_dailymix_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_CHART else 0 end) as yest_spotify_chart_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_SPOTIFY_OTHER else 0 end) as yest_spotify_other_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_LIBRARY else 0 end) as yest_apple_library_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_EXTERNAL else 0 end) as yest_apple_external_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_VOICE else 0 end) as yest_apple_voice_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_SEARCH else 0 end) as yest_apple_search_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_MUSICKIT else 0 end) as yest_apple_musickit_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_NOWPLAYING else 0 end) as yest_apple_nowplaying_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_DISCOVERY else 0 end) as yest_apple_discovery_streams, SUM(case when download_activity_date = '"""+yesterday_str+"""' then STREAMS_SOS_APPLE_OTHER else 0 end) as yest_apple_other_streams, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS else 0 end) as seven_total_streams, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then SKIPS else 0 end) as seven_total_skips, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then SAVES else 0 end) as seven_total_saves, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_PASSIVE else 0 end) as seven_total_streams_passive, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_ACTIVE else 0 end) as seven_total_streams_active, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_COLLECTION else 0 end) as seven_total_collection_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then SUB_TYPE_SUBSCRIPTION else 0 end) as seven_total_paid_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then SUB_TYPE_ADSUPPORTED else 0 end) as seven_total_free_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_COLLECTION else 0 end) as seven_spotify_collection_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_PLAYQUEUE else 0 end) as seven_spotify_playqueue_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_ALBUMPAGE else 0 end) as seven_spotify_album_page_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_ARTISTPAGE else 0 end) as seven_spotify_artist_page_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_SEARCH else 0 end) as seven_spotify_search_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_PLAYLISTS else 0 end) as seven_spotify_playlist_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_RELEASERADAR else 0 end) as seven_spotify_release_radar_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_DISCOVERWEEKLY else 0 end) as seven_spotify_discover_weekly_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_RADIO else 0 end) as seven_spotify_radio_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_DAILYMIX else 0 end) as seven_spotify_dailymix_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_CHART else 0 end) as seven_spotify_chart_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_SPOTIFY_OTHER else 0 end) as seven_spotify_other_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_LIBRARY else 0 end) as seven_apple_library_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_EXTERNAL else 0 end) as seven_apple_external_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_VOICE else 0 end) as seven_apple_voice_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_SEARCH else 0 end) as seven_apple_search_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_MUSICKIT else 0 end) as seven_apple_musickit_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_NOWPLAYING else 0 end) as seven_apple_nowplaying_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_DISCOVERY else 0 end) as seven_apple_discovery_streams, SUM(case when download_activity_date = '"""+seven_days_ago_str+"""' then STREAMS_SOS_APPLE_OTHER else 0 end) as seven_apple_other_streams from facts.prod.V_STREAMS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY where CONCAT(isrc, '_', country_code) IN """ + tracks_tuple + """ and download_activity_date >= '""" + seven_days_ago_str + """' and download_activity_date <= '""" + current_date_str + """' group by 1;""" ttq = """ select CONCAT(isrc, '_', country_code) as ISRC_GEO, sum(case when download_activity_date = '"""+current_date_str+"""' then creations else 0 end) as tt_creations, sum(case when download_activity_date = '"""+current_date_str+"""' then favorites else 0 end) as tt_favorites, sum(case when download_activity_date = '"""+current_date_str+"""' then likes else 0 end) as tt_likes, sum(case when download_activity_date = '"""+current_date_str+"""' then shares else 0 end) as tt_shares, sum(case when download_activity_date = '"""+current_date_str+"""' then views else 0 end) as tt_views, sum(case when download_activity_date = '"""+yesterday_str+"""' then creations else 0 end) as yest_tt_creations, sum(case when download_activity_date = '"""+yesterday_str+"""' then favorites else 0 end) as yest_tt_favorites, sum(case when download_activity_date = '"""+yesterday_str+"""' then likes else 0 end) as yest_tt_likes, sum(case when download_activity_date = '"""+yesterday_str+"""' then shares else 0 end) as yest_tt_shares, sum(case when download_activity_date = '"""+yesterday_str+"""' then views else 0 end) as yest_tt_views, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then creations else 0 end) as seven_tt_creations, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then favorites else 0 end) as seven_tt_favorites, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then likes else 0 end) as seven_tt_likes, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then shares else 0 end) as seven_tt_shares, sum(case when download_activity_date = '"""+seven_days_ago_str+"""' then views else 0 end) as seven_tt_views, from FACTS.PROD.V_TIKTOK_BY_ISRC_COUNTRY_DAILY where CONCAT(isrc, '_', country_code) IN """ + tracks_tuple + """ and download_activity_date >= '""" + seven_days_ago_str + """' and download_activity_date <= '""" + current_date_str + """' group by 1 """ return sq, ttq def get_daysT_q(tracks_tuple, geo, month_start, month_end): dayst_q = """select download_activity_date, CONCAT(isrc, '_', country_code) as ISRC_GEO, SUM(STREAMS_ACTIVE) as total_streams_active from facts.prod.V_STREAMS_BY_TRACK_COUNTRY_FEED_DISTRIBUTOR_DAILY where CONCAT(isrc, '_', country_code) IN """ + tracks_tuple + """ and download_activity_date >= '""" + month_start + """' and download_activity_date < '""" + month_end + """' and country_code = '""" + geo + """' group by 1, 2;""" return dayst_q