Skip to content

Commit

Permalink
Document ordering by an alias (#3828)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanl authored Feb 2, 2022
1 parent 7750b99 commit c149d96
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 1483,24 @@ defmodule Ecto.Query do
desc: c.id,
]
It's also possible to order by an aliased or calculated column:
from(c in City,
select: %{
name: c.name,
total_population:
fragment(
"COALESCE(?, ?) ? AS total_population",
c.animal_population,
0,
c.human_population
)
},
order_by: [
{:desc, fragment("total_population")}
]
)
## Expressions examples
City |> order_by([c], asc: c.name, desc: c.population)
Expand Down

0 comments on commit c149d96

Please sign in to comment.