## 11 Spotify Pull.r

{
    rm(Q)
    minDate <- "2012-09-01"
    dateCol <- "download_datetime"

    Q <- sprintf(paste0(
                  "SELECT  \n",
                  "        customerid AS userid,  \n",
                  "        country,  \n",
                  "        mobile,  \n",
                  "        MIN(%s) AS firstStream_byUCnDPMb, \n",
                  "        MAX(%1$s) AS lastStream_byUCnDPMb, \n",
                  "        DATE_TRUNC('month', %1$s) AS month, \n",
                  "        product,  \n",
                  "        count(*) AS total_streams_perCnDPUMb  \n",
                  "FROM staging_raw_spotify \n",
                  "WHERE %1$s >= %s \n",
                  "GROUP BY customerid, country, month, product, mobile  \n",
                  # "ORDER BY customerid, country, month, product, lastStream_byUCnDPMb, mobile  DESC \n"
                  "ORDER BY customerid, country, month, lastStream_byUCnDPMb ASC \n"
                ) , dateCol, minDate)

    ## Execute Query
    DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb <- runQry(Q)
    
    ## Check that there are rows in table (ie, no error)
    if (!nrow(DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb)) {
      err.msg <- "ERROR - No rows in DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb"
      notify(err.msg)
      stop(err.msg)
    }

    ## Otherwise, all is good. Notify.
    notify(paste("Qry complete:  ", dim(DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb, .txt=TRUE), "Beginning Saving"))

    ## Fix the capitalization of the one column: total_streams_perCnDPUMb
    setnames(DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb, "total_streams_perCnDPUMb",  "total_streams_perCnDPUMb")

    ## Save it
    jesusForData(DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb, info="RAW PULL")
    notify("Saving Complete. BACK TO WORK!")

    # ## this counts Conversions twice.  Once for each product
    # ProductByMobileByCountry.approx <- DT.Spotify.Counts.plusMAX_MIN_DATE.byUCnDPMb[, table(country, product, mobile, month)]
    # notify("Table Complete.\nALL DONE")
}