"Faithful Man" (886788689862) - iTunes WW EXCEPT NA
"Faithful Man" (886788474109) - No iTunes
"Faithful Man" (886788685833) - iTunes Version NA Only
"Faithful Man" (887396486010) - Music Video

UPCs <- c("886788689862", "886788474109", "886788685833", "887396486010")


sales outside US 

sale start date
units  since release came out 


TRACK .... UPC ..... COUNTRY .... UNITS     SALE / RETURN 

(LeeFields) Revo> headDB("dim_day", where="dayid > 4500 and dayid < 5000", n=25)[order(dayid)]
    dayid displaydate weekid monthid
 1:  4529  2011-05-25    648     150
 2:  4532  2011-05-28    648     150
 3:  4587  2011-07-22    656     152
 4:  4602  2011-08-06    658     153
 5:  4630  2011-09-03    662     154
 6:  4639  2011-09-12    664     154
 7:  4670  2011-10-13    668     155
 8:  4672  2011-10-15    668     155
 9:  4680  2011-10-23    670     155
10:  4682  2011-10-25    670     155
11:  4684  2011-10-27    670     155
12:  4707  2011-11-19    673     156
13:  4714  2011-11-26    674     156
14:  4735  2011-12-17    677     157
15:  4737  2011-12-19    678     157
16:  4743  2011-12-25    679     157
17:  4744  2011-12-26    679     157
18:  4775  2012-01-26    683     158
19:  4804  2012-02-24    687     159
20:  4808  2012-02-28    688     159
21:  4821  2012-03-12    690     160
22:  4861  2012-04-21    695     161
23:  4885  2012-05-15    699     162
24:  4900  2012-05-30    701     162
25:  4957  2012-07-26    709     164
    dayid displaydate weekid monthid

LEE.fact <- runQry("SELECT * from fact_analytics where artistid = 127288 AND dayid > 4808", verbose=TRUE)
DIM.countrys     <- runQry("SELECT * FROM dim_country")
DIM.transactions <- runQry("SELECT * FROM dim_transactiontype")

setkey(DIM.countrys,     countryid)
setkey(DIM.transactions, transactiontypeid)

## Merge in COUNTRY info 
setkey(LEE.fact, countryid)
LEE.fact[DIM.countrys, `:=`(countryname=countryname, country_code=country_code)]
LEE.fact[, countryid := NULL]

setkey(LEE.fact, transactiontypeid)
LEE.fact[DIM.transactions, `:=`(transaction_type=factor(transactiontypedesc))]
LEE.fact[, transactiontypeid := NULL]

print(LEE.fact)

jesusForData(LEE.fact)

setnames(LEE.fact, "releaseid", "upc")
setkey(LEE.fact, upc, country_code, storeid)

setcolorderpt(LEE.fact, key(LEE.fact))

print(LEE.fact)

UPCs <- as.numeric(UPCs)
LEE.No.USA <- LEE.fact[CJ(UPCs, setdiff(country_code, "US"))]

by.cols <- c("upc", "country_code", "countryname", "storeid", "trackid", "catalogid", "isrcid", "imprintid", "currencyid", "storeuserid")
SD.cols <-  c("units", "paidunits", "freeunits")


LEE.Agg <- LEE.No.USA[, lapply(.SD, sum, na.rm=TRUE), keyby=by.cols, .SDcols = SD.cols]
LEE.Agg <- LEE.Agg[units != 0]

LEE.Countries <- LEE.Agg[, lapply(.SD, sum, na.rm=TRUE), keyby=list(upc, country_code, countryname), .SDcols=SD.cols]

f.out1 <- as.path(outDir, "Lee_Countries.csv")
write.csv(x=LEE.Countries, file=f.out1)

f.out2 <- as.path(outDir, "Lee_Details.csv")
write.csv(x=LEE.Agg, file=f.out2)

reproduce(f.out2)
