-
Notifications
You must be signed in to change notification settings - Fork 6
SUM
SUM
returns the sum of one or more numbers.
SUM(arg1, [arg2, ...])
-
arg1
is a number -
arg2
is optional, and follows the same requirements asarg1
-
SUM
can take an arbitrary number of optional arguments
Let's say we're given a response with some vehicle information that looks like this:
{
"vehicle_info":{
"vin":"16m2flcb1",
"make":"Ford",
"model":"T",
"cylinders":2,
"horsepower":20,
"weight_kg":750
}
}
If we want an endpoint with an attribute called horsepower_plus_weight
, we're able to access and sum the keys of the JSON object like this:
SUM(vehicle_info.horsepower, vehicle_info.weight_kg)
Which returns 770
.
SUM(vehicle_info.horsepower, vehicle_info.horsepower) => 40
SUM(vehicle_info.cylinders) => 2
SUM(1, 2, 3, 4, vehicle_info.horsepower) => 30
For numerical functions (like SUM, AVERAGE, and ROUND), incoming values will be coerced into numbers where possible. If a key in the data source has been deleted (meaning no value is now returned), this will yield an error as a nil value cannot be coerced into a number.