Search code examples
elixirecto

Compose a query to get a complete record against max


In Ecto.Query.Api we use max in the select to get the max value in a field like this:

from q in jobs,
select: max(q.cost)

It will return the max value in the field.

If i want to return the complete record against that max value. I have to use where not select.

So how can i use max in where?

So In my understanding I think i have to use fragment in the where.

So what is the best possible solution?

Thanks


Solution

  • While one might construct a fragment using an overcomplicated where clause, the real task is really to get a topmost record when the table is ordered by cost:

    from q in jobs,
    order_by: [desc: q.cost],
    limit: 1