# SUM\_EACH

Stores the sum of values per group.

### Syntax

**`SUM_EACH([MAX VALUES, ]GROUP, VALUE)`**

### Arguments

**`MAX VALUES:`**   The maximum number of entries that can be counted (default: 2,147,483,647). \
&#x20;                             Beyond that, entries will be discarded. In some cases, depending on the data \
&#x20;                             distribution, discarding groups via **`MAX VALUES`** may cause some data to be \
&#x20;                             discarded from groups that would otherwise have been returned.  \
&#x20;                            **`MAX VALUES`** is optional. If it is omitted there is, in effect, no limit. \
&#xNAN;**`GROUP:`**              The field you would like counted.\
&#xNAN;**`VALUE:`**              The field that is summed. ‌

### Returns

A number

### Notes

**`SUM_EACH`** can be used only with hierarchical output types.\
&#xNAN;**`SUM_EACH`** returns a mapping of the key value where the key is the **`GROUP`**, and the value is the summation of the **`VALUE`** field over all the entries that have the same **`GROUP`** key. In the example below, all the "scores" for each "user" key are summed.



### Example

#### Data

```javascript
[
   {
      "user":"A01",
      "level":1,
      "score":25
   },
   {
      "user":"A01",
      "level":2,
      "score":48
   },
   {
      "user":"A01",
      "level":2,
      "score":81
   },
   {
      "user":"B02",
      "level":1,
      "score":23
   },
   {
      "user":"B02",
      "level":1,
      "score":29
   }
]
```

#### Query

Find the total score for each user:

```sql
SELECT
   SUM_EACH(data.user, data.score) AS sum_each_data_score__data_level 
FROM
   "MAX_SAMPLE_DATA - json"
```

#### Results

```javascript
{
   "sum_each_data_score__data_level":[
      {
         "key":"A01",
         "value":154
      },
      {
         "key":"B02",
         "value":52
      }
   ]
}
```

### Related Functions

[**SUM**](sum)

### Dialog

![](https://1488427404-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAbbZ6XL3iXMHb3pjqP%2Fuploads%2Fwnf2hiN94RkmR33SK00m%2FSUM_EACH_Dialog_1402x1048.jpg?alt=media\&token=2b64fda7-670e-489a-b55d-1c10b58b2f79)
