raquel_herr
Getting count across 4 different tables in Ecto
I’m trying to join 4 tables together. why does the following code only give me one row of the first table? For my final query, I’m trying to get the number of rows that have the same account_id from 4 different tables. There is only an association from each table to the CustodialAccount but not vice versa.
query =
from tli in TxLnIn,
join: tlo in TxLnOut,
on: tli.account_id == tlo.account_id,
join: toi in TxOnchainIn,
on: toi.account_id == tli.account_id,
join: too in TxOnchainOut,
on: too.account_id == tli.account_id,
where: tli.account_id == ^account_id,
select: %{
tx_ln_in_count: count(tli.id),
tx_ln_out_count: count(tlo.id),
tx_on_in_count: count(toi.id),
tx_on_out_count: count(too.id)
}
Repo.all(query)
Most Liked
kip
A couple of points to note:
-
The result of joining tables together is effectively another table that contains all the columns of the joined tables. Therefore you are really querying one table (the joined results), not four
-
Since you are selecting an aggregate function (
count) there result of theselectis just one row, the aggregate row -
countin Postgres at least ignoresNULLcolumns to thecountexpressions return the count of non-NULL columns
Hopefully that helps a little bit getting towards what you are after.
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









