Search code examples
elixirecto

Ecto: Adding Query results


I'm new to Phoenix and I have a points table. I'm trying to deduct one kind of points from another kind of points.

q1 = from p in Point, where: (p.i_id == ^user), select: sum(p.points)
claims = Repo.all(q1)
q2 = from c in Point, where: (c.r_id == ^user), select: sum(c.points)
points = Repo.all(q2)
balance = points - claims

But I get

bad arithmetic expression

How do I add or subtract query results?


Solution

  • Repo.all will always return a list, like this: [1].

    Use Repo.one instead, which will return a single value, e.g. 1.