Skip to main content

sum

The sum method will sum a numeric column for all found records in the query:

const sumOfAllScoresForAllGames = await Game.sum('score')

select sum("score") from "games"

NOTE: You must pass a column that can be summed, such as an integer or decimal. Summing non-numeric types will result in runtime exceptions.

Chaining

You can leverage chainable methods to filter out records at the SQL level before calling sum:

const sportsballScoresSum = await Game.where({ type: 'sportsball' }).sum('score')

select sum("score") from "games" where ("games"."type" ilike $1)