Dutch

Dutch

Ecto query sum avg

Hello, all! I have a noob question about ecto.query.
Table:

Fuzz
fizz_id	fizz_buzz
2	         1000
2	         1000
2	         1000
3	         3000
3	         3000
4	         1000

Take a look:

      Repo.all(
        from(n in Fuzz,
          group_by: n.fizz_id,
          select: avg(n.fizz_buzz)
        )
      )

The result that I get is [1000, 3000, 1000]. Can I get with query the result that I really want. In that example [1667]?

Most Liked

princemaple

princemaple

have you tried removing the group_by

wanton7

wanton7

Why do you have group_by there if you want to have average from all of them?

wanton7

wanton7

Then I don’t understand what is wrong because 1000, 3000, 1000 are averages per fizz_id?

fizz_id	fizz_buzz
2	         1000
2	         1000
2	         1000
= 3000 divided by 3 is 1000

3	         3000
3	         3000
= 6000 divided by 2 is 3000

4	         1000
= 1000 divided by 1 is 1000

Last Post!

wanton7

wanton7

Ok so you need average of averages. I haven’t used Ecto a lot and I’m not sure it supports select from select because it has to support range of databases. Inner select would be this query and outer select would return average of it. You might be able to solve this with a fragment of raw SQL. Or if there aren’t that many averages returned by that Ecto query you can always just use ä Elixir code to calculate that last average.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
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

We're in Beta

About us Mission Statement