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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
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