SUM

The sum command is used to calculate the total numeric value of a specific column in a table.

For Example:

To find the total sum of the salary column from the employees table.

select sum(salary) from employees;

Result:

The total sum of the salary column is 249300.

Ecto query for sum

In Ecto, you use the sum/1 function to achieve similar results.

sum/1

Expressions example

Consider the following example, which calculates the sum of the salary field from the HR.Employee schema.

HR.Employee
|> select([c], sum(c.salary))
|> HR.Repo.all()

Here, c is the reference variable that refers to HR.Employee. Refer Aliases in Ecto to know about aliases/reference variables.

Result:

The sum of the salary in HR.Job is 249300.

Keyword example

HR.Repo.all(from c in HR.Employee, select: sum(c.salary))