createSummaryOfAggTables <- function(dimensions, measures, tbl, dateCol, minDate, dateCol.max=dateCol, maxDate=NULL, OA_kCols={if (exists("OA_kCols", parent.frame())) get("OA_kCols", parent.frame())}, schema=NULL, drop.nulls=TRUE, cluster=NULL, verbose.simple=verbose, verbose=TRUE) {
## drop.nulls :  Some releaseid 's (if not in dim_release) are NULL.   This accounts for a very small part of total gross.  Drop these


  "20141203 -- TODO:   Use makeQuery()"

  hasMaxDate <- !(is.null(maxDate))
  stbl <- schemaPaste(schema=schema, tbl)

  ## Check that the table exists
  if(!qTableExists(tbl=tbl, schema=schema, cluster=cluster))
    stop (sprintf("\nTable '%s' does not exist on cluster %02i", schemaPaste(tbl=tbl, schema=schema), ifelse(is.null(cluster), getCluster(), cluster)))

  ## Check if any columns are missing 
  colsMissing <- setdiff(c(dimensions, measures), qShowCols(tbl, schema=schema, cluster=cluster))
  if (length(colsMissing))
    stop(warningCols(c("The following columns are missing from ", tbl), pasteQ(colsMissing, w="", q='"')))


# old 2015 #  ### Take a copy now so that the "AS .." portion does not make it into the GROUP BY statement
# old 2015 #  dimensions.groupby <- dimensions
# old 2015 #
# old 2015 #  ## Make the names into SQL appropriate names 
# old 2015 #  nms.dimensions <- names(dimensions)
# old 2015 #  if (!is.null(nms.dimensions)) {
# old 2015 #
# old 2015 #    ## most names will be blank, ie ("").  For those that are not, change dimension to  dimension as "dimension name"
# old 2015 #    notblank <- !is.na(nms.dimensions) & !(nms.dimensions == "" )
# old 2015 #
# old 2015 #    dimensions[notblank] <- sprintf('%s AS \"%s\"', dimensions[notblank], nms.dimensions[notblank])
# old 2015 #
# old 2015 #    ## 20141203 -- this seems to be causing a problem for c(store_name = "store_name_full", ..  ) in doimensions .... commenting out
# old 2015 #    # ## Also change the group by name to the new name
# old 2015 #    # dimensions.groupby[notblank] <- nms.dimensions[notblank]
# old 2015 #  }
# old 2015 #  
# old 2015 #  
# old 2015 #    ## Additional conditionals
# old 2015 #    ## Start with a blank clause
# old 2015 #    andClause <- ""
# old 2015 #    ## If dropping nulls, add that in
# old 2015 #    if (drop.nulls)
# old 2015 #      andClause <- paste0(andClause, "\n  AND NOT (releaseid is NULL) AND NOT (labelid is NULL)")
# old 2015 #    ## if there is a max date, add that
# old 2015 #    if (hasMaxDate)
# old 2015 #      andClause <- paste0(andClause, sprintf("\n  AND %s <= '%s'", dateCol.max, maxDate))
# old 2015 #  
# old 2015 #    ## Create the Query
# old 2015 #    colsSelecting <- pasteC(c(dimensions, sprintf("SUM(%s) AS %1$s", measures), sprintf("'%s' AS source", tbl)), C=",\n    ")
# old 2015 #    Qry <- setQry(sprintf("SELECT %s \nFROM %s \nWHERE %s >= '%s' %s\nGROUP BY %s", colsSelecting, stbl, dateCol, minDate, andClause, commaSep(dimensions.groupby)))

  browser(expr=inDebugMode("createSummaryOfAggTables"), text="in createSummaryOfAggTables() after names, before and-clause")

  Qry <- makeQry(tbl=tbl, schema=schema, colsToPull=c(dimensions, "source"=pasteQ(tbl, w="")), colsToAgg=measures, dateCol=dateCol, minDate=minDate, whereIn=if (drop.nulls) "NOT (releaseid is NULL) AND NOT (labelid is NULL)", maxDate=maxDate)

  ## Simple verbose output
  verboseMsg(verbose.simple, sprintf("  Querying  %s on %s using   \"%s\" >= %s %s", toupper(gsub("aggregated_", "", tbl)), ifelse(is.null(schema), yes="default schema", no=sprintf('"%s"', schema)), dateCol, minDate, ifelse(hasMaxDate, sprintf("  AND  \"%s\" <= %s", dateCol.max, maxDate), "") ), time=FALSE )

  ## EXECUTE QUERY
  Qres <- runQry(Qry, verbose=verbose, cluster=cluster, allow.large.groupby=26)

  ## Expressly set a column named date.  Note that this might duplicate the column if dateCol != "date"
  Qres[, date := as.Date(get(dateCol))]

  ## Set key, wrap in TRY, in case some columns are missing, dont want to fail
  try(setkeyIfNot(Qres, OA_kCols, verbose=FALSE, organize=TRUE), silent=FALSE)

  ## Add maxDate attribute
  setattr(Qres, "maxDate", if (hasMaxDate) maxDate else runQry(sprintf("SELECT max(%s) FROM %s", dateCol.max, stbl), verbose=FALSE, cluster=cluster))

  return(invisible(Qres))
}

