Don’t quote me on this, but I don’t think it is possible.
I have a vague recollection of trying to order some results depending on the result of an expression in the select, and the only way to make it work was to repeat the expression verbatim in the order_by clause.
Unless I’m wrong, you’ll have to do
from p in Payment, group_by: fragment("extract(hour from ?) as hour", p.inserted_at),
select: {
fragment(max("?", p.amount)),
fragment("extract(hour from ?) as hour", p.inserted_at)
}
and hope PostgreSQL is smart enough to compute that expression only once. Alternatively you can write a raw query.






















