cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Is it possible to perform group by and sum operation in Mapper/Conditional snap?

nirupama
New Contributor II

Is it possible to perform sum of the values by grouping them on few fields in mapper/conditional snap in snaplogic.
Because with aggregate snap Iโ€™m losing all input fields and keeping copy snap is taking more time to execute.

5 REPLIES 5

bojanvelevski
Valued Contributor

Hi @nirupama,

Thereโ€™s a group by functionality on the Aggregate snap that will allow you to have the input data in a group after youโ€™re done with your calculations (Aggregation).

Another option is to use a Group by N snap, with 0 in group size which will add all incoming objects to one array. After that, youโ€™ll need to add a Mapper snap with the following expression:

$array.reduce((acc, curr)=> acc + curr, 0)

You probably going to need to amend the expression per your needs. For example if you have an array of objects, and you need to calculate a specific field (ex. โ€œAgeโ€) the expression will look something like this:

$array.reduce((acc,curr)=> acc + curr.Age,0)

Regards,
Bojan

Hi @bojanvelevski ,

Thanks you. I tried with group by N(It does batching randomly), but I want to group them based on 3 fields say $A,$B,$C and then I want to do SUM on other field $D.Is there any such functionalities available in snaplogic using mapper?

Same thing applies to your use case, just the grouping is a little bit different and youโ€™ll have to use Group by Fields

@bojanvelevski I was trying with this approach, but getting below error.
image
expression using : $.reduce((acc,curr)=> acc + curr.INSTALLCUQTY,0)
Also, getting โ€˜nullโ€™ as output for sum.