sumBy
sumBy
returns the total sum for each group. Null values are ignored.
Applying this aggregation to a column where the sum cannot be computed will result in an error. For example, the sum is not defined for a column of string values.
Syntax
table.sumBy()
table.sumBy(groupByColumns...)
Parameters
Parameter | Type | Description |
---|---|---|
groupByColumns | String... | The column(s) by which to group data.
|
groupByColumns | ColumnName... | The column(s) by which to group data.
|
groupByColumns | Collection<String> | The column(s) by which to group data.
|
Returns
A new table containing the sum for each group.
Examples
In this example, sumBy
returns the sum of the whole table. Because a sum cannot be computed for the string column X
and Y
, these columns are dropped before applying sumBy
.
source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)
result = source.dropColumns("X", "Y").sumBy()
- source
- result
In this example, sumBy
returns the sum, as grouped by X
. Because a sum cannot be computed for the string column Y
, this column is dropped before applying sumBy
.
source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)
result = source.dropColumns("Y").sumBy("X")
- source
- result
In this example, sumBy
returns the sum, as grouped by X
and Y
.
source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)
result = source.sumBy("X", "Y")
- source
- result