I have a computed column like:
CREATE FUNCTION answer_vote(answer answer) RETURNS integer
LANGUAGE sql STABLE
AS $$
select sum(a.value)::integer from answer_vote a
where a.question = answer.question AND a.answer = answer.id
$$;
Is it possible to order on a computed column?
Yep! Using a procedure:
create function answers_ordered_by_vote() returns setof answer as $$
select *
from answer
order by answer_vote(answer)
$$ language sql stable;
My syntax may be wrong as it鈥檚 off the top of my head, but that鈥檚 the gist :wink:
I hoped for a more automatic solution. But will take that for now.
@calebmer @valorize has there been any update on this? i have a computed column that returns a SETOF so it's a bit more complicated, and wondering if there's any automated solution since this last arose?
@mattbretl Has been working on some plugins that achieve this. Pop into our discord and ask him about them: http://discord.gg/graphile
This is the second time in a month that I google this question and end up here, so for future me and others looking for an answer:
Ordering by scalar computed columns is supported by smart comment @sortable. You can check the PR here and the release notes here
Sadly this is not documented yet, I'm opening an PR here to the docs
Most helpful comment
This is the second time in a month that I google this question and end up here, so for future me and others looking for an answer:
Ordering by scalar computed columns is supported by smart comment
@sortable. You can check the PR here and the release notes hereSadly this is not documented yet, I'm opening an PR here to the docs