raquel_herr

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

kip

ex_cldr Core Team

A couple of points to note:

  1. 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

  2. Since you are selecting an aggregate function (count) there result of the select is just one row, the aggregate row

  3. count in Postgres at least ignores NULL columns to the count expressions return the count of non-NULL columns

Hopefully that helps a little bit getting towards what you are after.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

We're in Beta

About us Mission Statement