
ggplot(data=DT.EU_aggd_reshaped[(is_iTunesDA)], aes(x=deltaunits.pre_minus_prePre, y=deltaunits.wk1_minus_pre, color=TreatmentWk1)) + geom_point() + xlim(-20, 20) + gg_xyline()


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


tbl.raw <- "staging_raw_itunes"
dateCol.raw <- getDateColForTBL('staging_raw_itunes', schema="production")
product_coddes.using <- c("P", "I", "W", "Z") ## Remember, we just want to find out when the price changed
colsToPull.raw <- c(date=dateCol.raw, "upc", "country_code", "product_type_identifier", "sale_return", "promo_code", "preorder", "customer_currency", "customer_price", "royalty_currency", "royalty_price")

DT.vat_change <- runQry(makeQry(tbl=tbl.raw
                  # , colsToAgg="units" #colsToAgg
                  , colsToPull=colsToPull.raw
                  , dateCol=dateCol.raw
                  , minDate="2014-01-01"
                  , maxDate="2014-02-05"
                  , where=list( country_code=names(countries_using)
                              , product_type_identifier=product_coddes.using
                              )
                  , distinct=TRUE
                  , key="colsToPull"
                  ))
DT.vat_change[, customer_price := as.numeric(customer_price)]
DT.vat_change[, royalty_price  := as.numeric(royalty_price)]
DT.vat_change[, upc            := as.idcol(upc)]

DT.vat_change
DT.vat_change[country_code == "DE"]
 [, list(d=diffNA(date), date, r=diffNA(royalty_price)), keyby=list(royalty_currency, country_code, customer_price)] [r != 0]
DT.raw 

DT.vat_change[, .N, by=list(royalty_price, customer_price, country_code)][, .N, by=list(customer_price, country_code)][N != 1]
DT.vat_change[order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, date)] [, table(rdiff)]

DT.vat_change[, lunique(royalty_price), keyby=list(country_code, customer_price)][V1 > 1]
DT.vat_change[upc == 886443889736 & country_code == "GB"]


## DONT DELETE OR MODIFY THIS
DT.vat_change[order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][order(date), list(ddiff=diffNA(date), is_min=(if (length(date) > 1) diffNA(date) == minn(diffNA(date)) else FALSE), date), keyby=list(royalty_currency, country_code, customer_price, rdiff)][(is_min)]  [, table(date, customer_price)]
DT.raw[order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][order(date), list(ddiff=diffNA(date), is_min=(if (length(date) > 1) diffNA(date) == minn(diffNA(date)) else FALSE), date), keyby=list(royalty_currency, country_code, customer_price, rdiff)][(is_min)]  [, table(date, customer_price)]
## ----------------------------------------------------- ##

DT.raw[order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][order(date), list(ddiff=diffNA(date), is_min=(if (length(date) > 1) diffNA(date) == minn(diffNA(date)) else FALSE), date), keyby=list(royalty_currency, country_code, customer_price, rdiff)][(is_min)]  [date == "2015-01-21"]
DT.raw[date == "2015-01-21" & country_code == "DE" & customer_price == "8.99"]
order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][order(date), list(ddiff=diffNA(date), is_min=(if (length(date) > 1) diffNA(date) == minn(diffNA(date)) else FALSE), date), keyby=list(royalty_currency, country_code, customer_price, rdiff)][(is_min)]  [date == "2015-01-21"]


DT.raw[date < "2014-02-05"][order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][customer_price == "9.99"]
DT.raw[order(country_code, upc, date), list(rdiff=round(diffNA(royalty_price), 3), date), keyby=list(country_code, royalty_currency, customer_price, upc)] [rdiff != 0][customer_price == "9.99"]

DT.raw[upc == 888608412971 & country_code == "DE"]
DT.vat_change[upc == 888608412971 & country_code == "DE"]