Search.../

sum( )

Refines a WixDataAggregate to contain the sum from each aggregation group.

Description

The sum() function refines a WixDataAggregate to contain the sum of the values from the specified property for each aggregated group or from the whole collection if no group is defined.

When the aggregation is run, the returned WixDataAggregateResult object contains an item for each group with the following key:value pairs:

  • If a value was passed for the optional projectedName, the key is named using that value. Otherwise, the key is named using the following format: "propertyNameSum", where propertyName is the name of the specified property.
  • The value is the sum of the values found in the specified property.

Sums can only be calculated on properties of type Number.

Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.

Syntax

function sum(propertyName: string, [projectedName: string]): WixDataAggregate

sum Parameters

NAME
TYPE
DESCRIPTION
propertyName

string

The property in which to find the sum.

projectedName
Optional

string

The name of the property in the aggregation results containing the sum.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Add a sum aggregation

Code Example

Copy Code
1let newAggregate = aggregate.sum("population");
Add a sum aggregation with a projected name for the results

Code Example

Copy Code
1let newAggregate = aggregate.sum("population", "sumPopulation");
Create an aggregation, add a sum aggregation, and run it

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .sum("population")
8 .run()
9 .then( (results) => {
10 if (results.items.length > 0) {
11 let items = results.items; // see below
12 let numItems = results.length; // 6
13 let hasNext = results.hasNext(); // false
14 } else {
15 // handle case where no matching items found
16 }
17 } )
18 .catch( (error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 } );
22
23/* Given the sample data above, items is:
24 * [
25 * {
26 * "_id": {"state": "NY", "year": 2000},
27 * "populationSum": 8307000,
28 * "state": "NY",
29 * "year": 2000
30 * },
31 * {
32 * "_id": {"state": "FL", "year": 2000},
33 * "populationSum": 557000,
34 * "state": "FL",
35 * "year": 2000
36 * },
37 * {
38 * "_id": {"state": "NY", "year": 2010},
39 * "populationSum": 8453000,
40 * "state": "NY",
41 * "year": 2010
42 * },
43 * {
44 * "_id": {"state": "CA", "year": 2010},
45 * "populationSum": 4601000,
46 * "state": "CA",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2000},
51 * "populationSum": 4480000,
52 * "state": "CA",
53 * "year": 2000
54 * },
55 * {
56 * "_id": {"state": "FL", "year": 2010},
57 * "populationSum": 641000,
58 * "state": "FL",
59 * "year": 2010
60 * }
61 * ]
62 */
Create an aggregation, add a sum aggregation with a projected name for the results, and run it

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .sum("population", "totalPopulation")
8 .run()
9 .then( (results) => {
10 if (results.items.length > 0) {
11 let items = results.items; // see below
12 let numItems = results.length; // 6
13 let hasNext = results.hasNext(); // false
14 } else {
15 // handle case where no matching items found
16 }
17 } )
18 .catch( (error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 } );
22
23/* Given the sample data above, items is:
24 * [
25 * {
26 * "_id": {"state": "NY", "year": 2000},
27 * "totalPopulation": 8307000,
28 * "state": "NY",
29 * "year": 2000
30 * },
31 * {
32 * "_id": {"state": "FL", "year": 2000},
33 * "totalPopulation": 557000,
34 * "state": "FL",
35 * "year": 2000
36 * },
37 * {
38 * "_id": {"state": "NY", "year": 2010},
39 * "totalPopulation": 8453000,
40 * "state": "NY",
41 * "year": 2010
42 * },
43 * {
44 * "_id": {"state": "CA", "year": 2010},
45 * "totalPopulation": 4601000,
46 * "state": "CA",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2000},
51 * "totalPopulation": 4480000,
52 * "state": "CA",
53 * "year": 2000
54 * },
55 * {
56 * "_id": {"state": "FL", "year": 2010},
57 * "totalPopulation": 641000,
58 * "state": "FL",
59 * "year": 2010
60 * }
61 * ]
62 */