findDupRowsInSQLtable <- function(tbl, schema=NULL, byCols, snowflake_inuse=getOption("snowflake_inuse", FALSE), showVals=FALSE, minDate=NULL, maxDate=NULL, dateCol=NULL, wh=getSnowflakeWH(), dbname=getSnowflakeDB(), verbose=FALSE) {

  if (missing(byCols))
    stop("byCols cannot be missing")


  subqry <- makeQry(schema=schema, tbl=tbl, colsToPull=byCols, colsToAgg=c(row_count="*"), aggFunc="count", minDate=minDate, maxDate=maxDate, dateCol=dateCol, limit=NULL, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse)
  qry <- makeQry(schema=NULL, tbl=subqry, colsToPull="row_count", colsToAgg=c(occurrances="*"), aggFunc="count", limit=NULL, wh=wh, dbname=NULL, snowflake_inuse=snowflake_inuse)

  if (showVals) {
    qry.vals <- paste(subqry, "having row_count > 1 limit 100")
    catn("finding vals with row_count > 1")
    DT.vals <- runQry(qry=qry.vals, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, msg_sf=FALSE, verbose=TRUE)    
    obj <- timeStamp("DT.vals")
    assign(x=obj, value=copy(DT.vals), envir=globalenv())
    print(setIDCols(DT.vals))
    catn("Saved in globalenv (without setIDCols) as: ", obj)
  }

  DT.ret <- runQry(qry=qry, wh=wh, dbname=dbname, snowflake_inuse=snowflake_inuse, msg_sf=FALSE, verbose=verbose)
  DT.ret[, perc_of_all_rows := percOfTotal(occurrances)]

  DT.out <- copy(DT.ret)
  DT.out[, occurrances := formnumb(occurrances)]
  DT.out[, perc_of_all_rows := fwp(perc_of_all_rows)]
  print(DT.out)

  return(invisible(DT.ret))
}


if (FALSE) {
  findDupRowsInSQLtable("PARENT_UPC_VIEW", schema="bi", by="releaseid")


  findDupRowsInSQLtable(schema="production", tbl="product_subtype", byCols="id")
  findDupRowsInSQLtable(schema="bi", tbl="PARENT_UPC_VIEW", byCols="releaseid")
  findDupRowsInSQLtable(schema="bi", tbl="track_count_per_release_view", byCols="upc")

    # -- GENRE INFO -- 
  findDupRowsInSQLtable(schema="bi", tbl="genre_view", byCols="genreid")
  findDupRowsInSQLtable(schema="bi", tbl="release_subgenre", byCols="upc")
  findDupRowsInSQLtable(schema="bi", tbl="orchard_subgenres", byCols="orchard_id")
      # -- Artist & Label Metadata --
  findDupRowsInSQLtable(schema="bi", tbl="label_view", byCols="labelid")
  findDupRowsInSQLtable(schema="bi", tbl="artist_view", byCols="artistid")
  findDupRowsInSQLtable(schema="production", tbl="dim_subaccount", byCols="subaccountid")

  findDupRowsInSQLtable(schema="bi", tbl="release_with_metadata_view", byCols="releaseid")


  findDupRowsInSQLtable(schema="spotify", tbl="sos_from_raw_view", byCols="row_id", minDate='2016-01-01', dateCol="activity_datetime", showVals=FALSE, verbose=TRUE)

  tbl_joined <- "prod.production.staging_raw_spotify_v2 AS S
          LEFT JOIN prod.production.dim_track AS production_dim_track ON production_dim_track.isrc = S.isrc
          LEFT JOIN prod.bi.release_with_metadata_view AS bi_release_with_metadata_view ON bi_release_with_metadata_view.releaseid = production_dim_track.upc
          "
  findDupRowsInSQLtable(schema=NULL, tbl=tbl_joined, byCols="row_id", minDate='2016-01-01', dateCol="tmstamp")


  DT.row <- sfQry("SELECT * FROM spotify.sos_from_raw_view where row_id = 174019425566")
  unique(DT.row, by=NULL)

  DT.prob <-   setIDCols(sfQry("SELECT * FROM production.dim_track where isrc = 'ARA170800026'"))[]


}
