insideac
Sum of two left_joins returns doubled values in first table
I have the following query:
{account, {inflow, outflow}} =
Repo.one(from account in App.Account,
left_join: t0 in App.Transaction,
on: t0.account_id == account.id
and t0.user_id == ^current_user.id
and t0.deleted == false
and t0.type == "inflow",
left_join: t1 in App.Transaction,
on: t1.account_id == account.id
and t1.user_id == ^current_user.id
and t1.deleted == false
and t1.type == "outflow",
where: account.id == ^id and t1.user_id == t0.user_id,
group_by: account.id,
select: {account, {sum(t0.amount), sum(t1.amount)}})
and though the second join is coming back as expected, the first table seems to be doubling up on the SUM. For example, if there is one transaction of 1,000, it will instead return 2,000, plus whatever the right amount of the second table’s total is.
Not sure what could be going on here, any help is much appreciated!
Most Liked
peerreynders
What you are trying to accomplish is usually done with a UNION in SQL, not JOIN. Roughly:
SELECT account_id, amount as inflow, NULL as outflow FROM transaction WHERE type = 'inflow' AND account_id = ACCOUNT_ID
1, 1000, NULL
1, 1000, NULL
SELECT account_id, NULL as inflow, amount as outflow FROM transaction WHERE type = 'outflow' AND account_id = ACCOUNT_ID
1, NULL, 500
1, NULL, 1500
(
SELECT account_id, amount as inflow, NULL as outflow FROM transaction WHERE type = 'inflow' AND account_id = ACCOUNT_ID
) UNION ALL (
SELECT account_id, NULL as inflow, amount as outflow FROM transaction WHERE type = 'outflow' AND account_id = ACCOUNT_ID
)
1, 1000, NULL
1, 1000, NULL
1, NULL, 500
1, NULL, 1500
SELECT account_id, SUM(inflow), SUM(outflow) FROM (
SELECT account_id, amount as inflow, NULL as outflow FROM transaction WHERE type = 'inflow' AND account_id = ACCOUNT_ID
) UNION ALL (
SELECT account_id, NULL as inflow, amount as outflow FROM transaction WHERE type = 'outflow' AND account_id = ACCOUNT_ID
)
GROUP BY account_id
1, 2000, 2000
Ecto doesn’t support unions so the easiest alternative is two separate queries - one for inflow another for outflow.
benwilson512
Sort of. If you’re doing a preload with a join, the rows come out of the database with duplicates, and then Ecto de-duplicates them. There are plenty of situations where Ecto doesn’t de-duplicate stuff, ie:
iex(2)> length Repo.all from u in User
401
iex(3)> length Repo.all from u in User, join: m in assoc(u, :memberships)
674
Will return multiple copies of the same user if they have have more than one membership. This is a good thing really, it provides a nice SQL like experience.
In a way though the OP’s question is a whole other scenario. The duplication happens at the SQL level, and so does the aggregation, so there isn’t even an opportunity for Ecto to deduplicate even if it wanted to.
michalmuskala
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")})
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









