I am trying to call a function in Postgres, from within my sql statement. It gives me a "column xxx not found" error. My SQL code is below.
SELECT r.resource_id, r.name, owner.name, r.status,
r.cost, r.display_unit, r.next_available_date,
geodistance(r.latitude, r.longitude, 41.824, 71.4128) as distance
FROM resource_item as r
INNER JOIN base_user AS owner
ON r.resource_owner = owner.username
WHERE distance < 100
ORDER BY distance, r.next_avail_date, r.name;
I get the following error:
ProgrammingError: column "distance" does not exist
The function geodistance() itself has been tested as an individual statement like the one below and works fine.
select geodistance(38.898556, -77.037852, 38.897147, -77.043934) as distance from incident;
I am able to get a response if I put the geodistance() function in the WHERE clause, but I don't know how to do the ORDER BY.
I am stuck a bit. I need to evaluate the distance and be able to compare with an input value. How can I achieve this?