
# First ensure character columns where needed
if (!is.character(DB.all.count[["upc"]]))
  DB.all.count[, upc := as.idcol(upc)]

if (!is.character(DB.all.count[["user_country"]]))
  DB.all.count[, user_country := as.character(user_country)]



# Note that the data that comes from the SQL query is wide whereas we need it in a long format. 
# So as part of the cleaning process, we will stack the rows

## I want to stack up the u & t columns and add a column for "measuring"
##   
##       ## FROM THIS ##
##               upc user_country u_count t_count   store month
##   1: 884977928945           US  1975658 12621966 Spotify   Sep
##   2: 884977928945           US  1930239 12421890 Spotify   Aug
##   
##       ## TO THIS ##
##                upc user_country    count   store month measuring
##    1: 884977928945           US  1975658 Spotify   Sep    unique
##    2: 884977928945           US 12621966 Spotify   Sep     total
##    3: 884977928945           US  1930239 Spotify   Aug    unique
##    4: 884977928945           US 12421890 Spotify   Aug     total
##    

## IF 'u_count' is present, then reshape.  Otherwise, just rename t_count to count.
if ("u_count" %in% names(DB.all.count)) {
  # These will be the .SD columns
  no.t_count <- setdiff(names(DB.all.count), "t_count")
  no.u_count <- setdiff(names(DB.all.count), "u_count")

  # Reshape the data
  DB.all.count <- 
  	rbind(
           DB.all.count[, c(setnames(.SD, "u_count", "count"), list(measuring="unique")), .SDcols=no.t_count  ]
       ,   DB.all.count[, c(setnames(.SD, "t_count", "count"), list(measuring="total" )), .SDcols=no.u_count  ]
  	)

  rm(no.t_count, no.u_count)
} else {
  setnames(DB.all.count, "t_count", "count")
}
