drikanius

drikanius

Count unique ids inside aggregation

Sup

I have a Property module with the relationship:

relationships do
 #...

  has_many :offers, Offer

 #...
end

I would like to do something like:

  aggregates do
    count :total_unique_offers, :offers do
     # filter distinct(:offeror_id) ????
    end
  end

I have this calculation, which already works, but it doesn’t seem super optimized.

Is there a way to perform this calculation via aggregate?

defmodule TotalUniqueOffers do
  @moduledoc false

  use Ash.Calculation

  @impl true
  def load(_query, _opts, _context), do: [:offers]

  @impl true
  def calculate(properties, _, _) do
    Enum.map(properties, fn %{offers: offers} ->
      offers
      |> Enum.uniq_by(& &1.offeror_id)
      |> Enum.count()
    end)
  end
end

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash
count :total_unique_offers, :offers do
  field :offerer_id
  uniq? true
end

Also Liked

drikanius

drikanius

Fits like a glove :bowing_man:.

Thank you so much!!!

Last Post!

drikanius

drikanius

Fits like a glove :bowing_man:.

Thank you so much!!!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement