How to get a row by id with row number in Ecto?

Add virtual field to Ship schema

field(:number, :integer, virtual: true)

And use the following query:

ships_indices_query =
  from s in Ship, select: %{s | number: row_number() |> over(order_by: s.id)}

query = 
  from si in subquery(ships_indices_query), where: si.id == ^id
6 Likes