Skip to main content

avg

The avg method returns the average (arithmetic mean) of a set of numeric values:

const avgOfAllScoresForAllGames = await Game.avg('score')

select avg("score") from "games"

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

Chaining

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

const sportsballScoresAvg = await Game.where({ type: 'sportsball' }).avg('score')

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