Function - Cumulative Function

2 - Example

2.1 - Oracle Sql

SELECT 
    SUM(SAL) OVER (ORDER BY DATE) CUMSUM,
    SUM(SAL) OVER (PARTITION BY ACCOUNT ORDER BY DATE) CUMSUM_OVER_ACCOUNT,
FROM 
...

2.2 - R

With a R - Data Table

ledgerExpenseCumulative <- ledgerExpense[,.(amount=sum(amount)),keyby=.(account,date)
    ][,.(date=.SD$date, amount=.SD$amount,cumAmount=cumsum(.SD$amount)),by=.(account)]

where:

  • the first pass calculate the sum of an account for a date and order the set
  • the second pass calculate the cumulatif sum on the partition (.SD) by account
code/function/cumulative.txt · Last modified: 2017/12/11 11:33 by gerardnico