NOTE TO SELF: this is the "old" and incorrect version of DB.using.old Specifically, it had a dateMin & dateMax in the query, which should not be there the date rnages are for the RELEASE dates, ie, for which UPCs to use. Once the UPCs have been identified, ALL downloads for that release should be pulled (unless there is a limit on the download dates for a different reason) if (!exists("origin")) origin <- "1970-01-01" # Pre-Clean ## ------------------------------ ## try(dbDisconnect(con), silent=TRUE) try(detach(package:RPostgreSQL), silent=TRUE) library(RPostgreSQL) ## ------------------------------ ## ## ------------------------------ ## ## Query ## ------------------------------ ## upcusings <- DB.all.meta[!is.na(releaseDate) & !is.na(upc) ][ releaseDate >= minReleaseDate & releaseDate <= maxReleaseDate, unique(upc)] ## TODO: Check Globally QRY.DB.using.old <- paste("SELECT upc, download_date, COUNT(customerid) AS count, country AS user_country", "FROM production.staging_raw_spotify", "WHERE upc in ", pasteQ(upcusings), "AND download_date >=", pasteQ(minReleaseDate), "AND download_date <=", pasteQ(maxReleaseDate), "AND country = 'US'", "GROUP BY upc, download_date, country") # ~ About 1 second per day. DB.using.old <- runQry(qry=QRY.DB.using.old, verbose=TRUE) ## ------------------------------ ## # Cleanup Again ## ------------------------------ ## try(dbDisconnect(con), silent=TRUE) try(detach(package:RPostgreSQL), silent=TRUE) ## ------------------------------ ## # First ensure character columns where needed if (!is.character(DB.using.old[["upc"]])) DB.using.old[, upc := as.idcol(upc)] if (!is.character(DB.using.old[["user_country"]])) DB.using.old[, user_country := as.character(user_country)] setkeyIfNot(DB.using.old, "upc") # ---------------------------- matchKey(DB.using.old, DB.all.meta, "upc") DB.using.old[DB.all.meta, `:=`(artist=artist , release=release , mkt_priority=mkt_priority , genre=genre , releaseDate=releaseDate) ] # Clean up Date Cols DB.using.old[, releaseDate := as.Date(releaseDate, origin=origin)] # ---------------------------- jesusForData(DB.using.old, git=TRUE) # ----------------------------