### Pulls every purchase done by user and orders them by date(rn)
with data as (
select eventTimestamp
, eventName
, userID
, chipsBalance as chipsBalanceAfter
, lag(chipsBalance, 3) over (partition by userID order by eventTimestamp asc )as chipsBalanceBefore
, realCurrencyAmount / 100 as realCurrencyAmount
, productID
, productType
, userLevel
, gaUserStartDate
, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY eventTimestamp ASC) AS rn
from events_live
where chipsBalance is not null
and eventTimestamp >='2019-04-01' and gaUserStartdate >= '2019-04-01' and gaUserStartdate <= '2020-02-01'
)
select eventTimestamp, userID, realCurrencyAmount, productID, chipsBalanceBefore, rn, gauserstartdate, userLevel, productType
from data
where realCurrencyAmount > 0
group by 1, 2, 3, 4, 5, 6, 7, 8, 9