stop("This is NOT the file you are looking for.\nThis file simply compres the erorrs with the fact table.\n You are probably looking for the SQL file\n'~rsaporta/git/orch/src/DeNormalizing/BI_tables/Analytics/02b fa_errors_view.sql'\n")

nms.anal <- qShowCols("fact_analytics")
nms.err <- qShowCols("fact_analytics_error")

compare(nms.anal, nms.err)

setdiff(nms.anal, nms.err)
intersect(nms.anal, nms.err)
setdiff(nms.err, nms.anal)

"download_activity_date" "royaltydollar"          "storeuserid"

"royaltydollar" .. royaltyusd

DT.Errors <- runQry(m("fact_analytics_error", schema="production", colsToPull=c("storeid", month=sql1stOfMonth("downloaddate"), "transactiontypeid"), colsToAgg=c("paidunits", "royaltyusd"), minDate="2014-01-01", dateCol="downloaddate"), cluster=9)

DT.fa <- runQry(m("fact_analytics", schema="production", colsToPull=c("storeid", month=sql1stOfMonth("download_activity_date"), "transactiontypeid"), colsToAgg=c("paidunits", "royaltydollar"), minDate="2014-01-01", dateCol="download_activity_date"), cluster=9)

## TODO:  rename each Cols To Agg according to table and use MERGE

kCols <- c("month", "storeid", "transactiontypeid")
matchKey(DT.Errors, DT.fa, key=kCols, organize=TRUE)


DT.fa[DT.Errors, paidunits.missing := i.paidunits]
DT.fa[, paidunits.missing := removeNA(paidunits.missing, 0)]
DT.fa[, perc.present := paidunits / (paidunits + paidunits.missing)]
DT.fa[, perc.missing := paidunits.missing / (paidunits + paidunits.missing)]

DT.fa[, perc.miss_rel_to_pres := paidunits.missing / paidunits]

addTransacInfo_(DT.fa)

jul <- as.Date("2014-07-01")
aug <- as.Date("2014-08-01")
sep <- as.Date("2014-09-01")
percCols <- extract("perc\\.", DT.fa)

write.csv(DT.fa[.(c(jul, aug, sep), 1), lapply(.SD, as.numeric), .SDcols=percCols, by=list(month, transac_type)], file=out.p("iTunes_Unmatch_Data.csv"))

DT.fa[.(jul, 1)]
DT.Errors[.(jul, 1)]

jesusForData(DT.fa)
jesusForData(DT.Errors)
jesusForData(DT.transacs)