Supposing there is query, which calculates distance between given point and points stored in database. The purpose is to find few closest points to a given one
select x,y, get_distance(25,13,x,y) as dis
from points
order by dis
But as a condition, i'd like to set restriction for distance in order to get only results with distance <1000
select x,y, get_distance(25,13,x,y) as dis
from points
where dis <1000
order by dis
result of this query is : ERROR: column "dis" does not exist
So i'm forced to write function call twice:
select x,y, get_distance(25,13,x,y) as dis
from points
where get_distance(25,13,x,y) <1000
order by dis
Althought, function get_distance is declared as immutable and does not compute twice as declared in docs, the query itself looks ugly.
Is there any way to use result of get_distance as dis in condition and not to write it twice?