r - How do I create a running count of outcomes sequentially by date and unique to a specific person/ID? -


i have list of unique customers have made transactions on year (jan – dec). have bought products using 3 different methods (card, cash, check). goal build multi-classification model predict method pf payment.

to engineering recency , frequency features training data, having trouble following frequency count because way know how in excel using countifs , sumifs functions, inhibitingly slow. if can and/or suggest solution, appreciated:

so have data set 3 columns (customer id, purchase date, , payment type) sorted purchase date customer id. how prior frequency count of payment type date not include count of current row transaction or future transactions > purchase date. want running count of each payment option, based on unique customer id, , date range < purchase date of training row. in head see “crawling” backwards through transactions , counting. simplified screenshot of data frame below 3 prior count columns looking generate programmatically.

screenshot

this gives answer list of customerid, purchasedate, paymentmethod , prior counts

select customerid, purchasedate, paymentmethod,            (            select count(customerid) history t                             t.customerid=history.customerid                  , t.paymentmethod=history.paymentmethod                  , t.purchasedate<history.purchasedate            )           priorcount history; 

you can save query , use source crosstab query columnar format want

some notes:

  • i assumed "history" source table name - can change query above use correct source
  • to use query, open new query in design view. close window asks tables query built on. open sql view of query design - design view, shows sql instead of normal design interface. copy above sql view.
  • you should able switch datasheet view , see results
  • when query working satisfaction, save appropriate name
  • open new query in design view
  • when list of tables include, switch list of queries , include query saved
  • change query type crosstab , update query needed select rows, columns , values - "access crosstab queries" if need more help.

another tip see happening here:

  • you can take subquery - parts inside () above - , make statement it's own query, excluding opening , closing (). can @ it's design view see does
  • save appropriate name , put query above in place of statement in () - can @ design view.

sometimes it's easier visualize , learn 2 queries strung way work sub queries.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -