aggSum

Returns the sum of all values in a set.

Data

{
  "cars": [
    {"brand":"Lu","qty":1,"sort":1},
    {"brand":"Fa","qty":4,"sort":4},
    {"brand":"Vi","qty":3,"sort":3},
    {"brand":"Fa","qty":2,"sort":2},
    {"brand":"To","qty":1,"sort":1},
    {"brand":"Vi","qty":10,"sort":5}
  ]
}

Total: {d.cars[].qty:aggSum}

Total by sorting "sort" values above 1: {d.cars[sort>1].qty:aggSum}

Total by multiplying the "qty" by the "sort" attribute: {d.cars[sort>1].qty:mul(.sort):aggSum:formatC}

The aggSum aggregator can be used within loops, for instance:

  • sum-up people's salaries by departments: {d.departments[i].people[].salary:aggSum} {d.departments[i].people[].salary:aggSum(.i)} (alternative)

  • global sum of all departments, all people's salary: {d.departments[i].people[i].salary:aggSum} {d.departments[i].people[i].salary:aggSum(0)} (alternative)

Change the partition with dynamic parameters:

  • sum salaries by people by age, regardless of departments {d.departments[i].people[i].salary:aggSum(.age)}

  • sum salaries by people by age and gender, regardless of departments {d.departments[i].people[i].salary:aggSum(.age, .gender)}

Last updated