Upsolver
Search…
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). Beyond that, entries will be discarded. In some cases, depending on the data distribution, discarding groups via MAX VALUES may cause some data to be discarded from groups that would otherwise have been returned. MAX VALUES is optional. If it is omitted there is, in effect, no limit. GROUP: The field you would like counted. VALUE: The field that is summed. ‌

Returns

A number

Notes

SUM_EACH can be used only with hierarchical output types. 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

1
[
2
{
3
"user":"A01",
4
"level":1,
5
"score":25
6
},
7
{
8
"user":"A01",
9
"level":2,
10
"score":48
11
},
12
{
13
"user":"A01",
14
"level":2,
15
"score":81
16
},
17
{
18
"user":"B02",
19
"level":1,
20
"score":23
21
},
22
{
23
"user":"B02",
24
"level":1,
25
"score":29
26
}
27
]
Copied!

Query

Find the total score for each user:
1
SELECT
2
SUM_EACH(data.user, data.score) AS sum_each_data_score__data_level
3
FROM
4
"MAX_SAMPLE_DATA - json"
Copied!

Results

1
{
2
"sum_each_data_score__data_level":[
3
{
4
"key":"A01",
5
"value":154
6
},
7
{
8
"key":"B02",
9
"value":52
10
}
11
]
12
}
Copied!
SUM

Dialog

Last modified 8mo ago