
## THIS IS THE OLD VERSION
if (FALSE) {

# ------------------------------------------------ #
#    ~~~~~~    QUERY-MAKING FUNCTIONS    ~~~~~~    #
# ------------------------------------------------ #
  makeSpotifyQuery <- function(monthsUsing) {
    QRYS <- c()
    for (endMonth in month.abb[monthsUsing]) {

      # The 12 Week period ending on the MONDAY on or following the last day of the month.
      dateRange <- weekSpan(weeks=12, end=endMonth, round.end="Mon", round.direction="future", range=TRUE)

      QRYS[[paste0("Spot_", endMonth)]] <- 
      paste0("
      SELECT   upc 
             , country AS user_country
             , COUNT (DISTINCT customerid) AS u_count
             , COUNT (customerid)          AS t_count
            , 'Spotify' AS store
            , ", paste0("'", endMonth, "'"), "  AS month
      FROM  production.staging_raw_spotify
      WHERE download_date >= '", dateRange[["start"]], "'
        AND download_date <= '", dateRange[["end"]], "'
      GROUP BY upc, country
      ")
    }

    return(QRYS)

  }

  makeiTunesQuery <- function(monthsUsing) {
    QRYS <- c()
    for (endMonth in month.abb[monthsUsing]) {

      # The 12 Week period ending on the MONDAY on or following the last day of the month.
      dateRange <- weekSpan(weeks=12, end=endMonth, round.end="Mon", round.direction="future", range=TRUE)

      QRYS[[paste0("iTun_", endMonth)]] <- 
    paste0("
     SELECT upc
          , user_country
          , SUM (netUnits) AS  u_count
          , COUNT (netUnits) AS  t_count
          , 'iTunes' AS store
          , ", paste0("'", endMonth, "'"), "  AS month
      FROM     (SELECT upc, customer_identifier, country_code AS user_country,
                       SUM ( units *  ((-1) ^ ((sale_return = 'R') IS TRUE)::int)  )  AS netUnits
                FROM  production.staging_raw_itunes
                WHERE download_date >= '", dateRange[["start"]], "'
                  AND download_date <= '", dateRange[["end"]], "'
                GROUP BY upc, country_code, customer_identifier
               ) ", paste0("mini_", endMonth) ,"
      GROUP BY upc, user_country
      "
      )
    }
    return(QRYS)
  }
# ------------------------------------------------ #

{
  
  # monthsUsing param set in 00

  utilSource(verbose=TRUE)

  ## --- CONSTRUCT QUERY  ----- ##
  SpotifyQry <- makeSpotifyQuery(monthsUsing)
  iTunesQry <-  makeiTunesQuery(monthsUsing)

  QRYS <- c(SpotifyQry, iTunesQry)

  # OLD:   nms.q <- names(QRYS)
  # OLD:   if (is.null(nms.q) || !all(nchar(nms.q)> 0) || anyDuplicated(nms.q))
  # OLD:      stop("`QRYS` does not have proper names")
  # OLD:   QRYS <- paste0("(", QRYS, ")  ", nms.q)
  # OLD:   QRYS[[1L]] <- paste0("SELECT * FROM ", QRYS[[1L]], " ")
  # OLD:   QRYS[-1L] <- paste("\nFULL OUTER JOIN\n", QRYS[-1L], "\nUSING (upc, user_country)\n")
  # OLD: 
  # OLD:   QRY <- pasteC(QRYS, C=" ")
  
  QRY <- pasteC(QRYS, C="\nUNION\n")
}

} # // END OF OLD VERSION

# ---------------------------- 

# ~~~~~~~~  NEW QRY   ~~~~~~~~~~~ #

  makeSpotifyQuery <- function(monthsUsing, dateMin, dateMax, incl.unique=FALSE) {
    ## TODO:  unique counts need to be better calculated.  
      paste0("
      SELECT   upc 
             , download_date
             , country AS user_country",
             # ", COUNT (DISTINCT customerid) AS u_count",
             ", COUNT (customerid)          AS t_count
            , 'Spotify' AS store
      FROM  production.staging_raw_spotify
      WHERE download_date >= '", as.character(dateMin), "'
        AND download_date <= '", as.character(dateMax), "'
      GROUP BY upc, country, download_date
      ")
  }


  utilSource(verbose=TRUE)
  QRY <- makeSpotifyQuery(monthsUsing, dateMin=minReleaseDate, dateMax=Sys.Date())