3.2 maccum Cumulative Calculation

Calculates the cumulative value for the column specified at f= parameter and save the result in a new column. Cumulative calculation is carried by the key unit when k= is specified.

Format

maccum f= [s=] [k=] [i=] [o=] [-nfn] [-nfno] [-x] [-q] [precision=] [--help] [--version]

Parameter

f=

Specify the field(s) (multiple fields can be specified) for cumulative calculation.

 

Field with NULL values is ignored.

 

Use :(colon) to specify new field name. Example: f=quantity:cumulativeqty

s=

After sorted by specified field (multiple fields can be specified), calculate cumulative value.

 

s= parameter is required if -q option is not specified.

k=

Specify the field name as unit for cumulative calculation (multiple fields can be specified).

Example

Example 1: Basic Example

Calculate the cumulative values ​​of "Quantity" and "Amount" fields for each "Customer", save output as new data attributes in new columns named "AccumQuantity" and "AccumlAmount".

$ more dat1.csv
Customer,Quantity,Amount
A,1,10
A,2,20
B,1,15
B,3,10
B,1,20
$ maccum s=Customer f=Quantity:AccumQuantity,Amount:AccumAmount i=dat1.csv o=rsl1.csv
#END# kgaccum f=Quantity:AccumQuantity,Amount:AccumAmount i=dat1.csv o=rsl1.csv s=Customer
$ more rsl1.csv
Customer%0,Quantity,Amount,AccumQuantity,AccumAmount
A,1,10,1,10
A,2,20,3,30
B,1,15,4,45
B,3,10,7,55
B,1,20,8,75

Example 2: Specify Calculation by Key

Calculates the cumulative value of "Quantity" and "Amount" fields for each "Customer", and save the output in new columns named "AccumQuantity" and "AccumAmount".

$ more dat1.csv
Customer,Quantity,Amount
A,1,10
A,2,20
B,1,15
B,3,10
B,1,20
$ maccum k=Customer s=Customer f=Quantity:AccumQuantity,Amount:AccumAmount i=dat1.csv o=rsl2.csv
#END# kgaccum f=Quantity:AccumQuantity,Amount:AccumAmount i=dat1.csv k=Customer o=rsl2.csv s=Customer
$ more rsl2.csv
Customer,Quantity,Amount,AccumQuantity,AccumAmount
A,1,10,1,10
A,2,20,3,30
B,1,15,1,15
B,3,10,4,25
B,1,20,5,45

Example 3: Cumulative computation with NULL values

Calculate the cumulative values ​​of "Quantity" and "Amount" item, and save the output as new columns named "AccumQuantity" and "AccumAmount". NULL values are ignored. Records with NULL values will be retained in the output.

$ more dat2.csv
Customer,Quantity,Amount
A,1,10
A,,20
B,1,15
B,3,
B,1,20
$ maccum s=Customer f=Quantity:AccumQuantity,Amount:AccumAmount i=dat2.csv o=rsl3.csv
#END# kgaccum f=Quantity:AccumQuantity,Amount:AccumAmount i=dat2.csv o=rsl3.csv s=Customer
$ more rsl3.csv
Customer%0,Quantity,Amount,AccumQuantity,AccumAmount
A,1,10,1,10
A,,20,,30
B,1,15,2,45
B,3,,5,
B,1,20,6,65

Related Commands

mshare : Calculate composition ratio. Cumulative relative frequency can be calculated when used with maccum.

mcal : Cumulative total can be calculated by using the results from the previous row with #{}.