Given the following schema, I want to compute :games_won / :games_played, populate it into :percentage_won and sort by :percentage_won. I managed to compute the value if I use select_merge and leave out "AS ?", but how do I reference this computed column in order_by?
schema "players" do
field :name, :string
field :games_played, :integer
field :games_won, :integer
field :percentage_won, :float, virtual: true
timestamps()
end
I tried the following query:
def list_players(sort_by, sort_order) do
query =
from(p in Player,
select_merge: %{percentage_won: fragment("(?::decimal / NULLIF(?,0)) AS ?", p.games_won, p.games_played, p.percentage_won)},
order_by: [{^sort_order, field(p, ^String.to_atom(sort_by))}])
Repo.all(query)
end
But calling list_players("percentage_won", :asc) gives me the following error:
** (Ecto.QueryError) ...:28: field `percentage_won` in `select` is a virtual field in schema Player in query:
from p0 in Player,
order_by: [asc: p0.name],
select: merge(p0, %{percentage_won: fragment("(?::decimal / NULLIF(?,0)) AS ?", p0.games_won, p0.games_played, p0.percentage_won)})