Sum of two left_joins returns doubled values in first table

FWIW in Ecto master there’s support for the filter expressions. Using them, this could be achieved with a simpler query:

{account, inflow, outflow} =
     Repo.one(from a in App.Account,
       left_join: t in App.Transaction,
       on: t.account_id == a.id and t.user_id == ^current_user.id and not t.deleted,
       where: a.id == ^id,
       group_by: a.id,
       select: {a, filter(sum(t.amount), t.type == "inflow"), filter(sum(t.amount), t.type == "outflow")})
2 Likes