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

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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 42576 114
New

We're in Beta

About us Mission Statement