9

I am trying to use result of function execution in where clause but with no success:

SELECT clinics.*, distance_between_objects(1, id, 7, 3) AS dist FROM clinics WHERE dist<=1;

gives me:Column "dist" does not exists. Quoting it like:

SELECT clinics.*, distance_between_objects(1, id, 7, 3) AS dist FROM clinics WHERE "dist"<=1;

doesn't helps either. Please advise is there possibility in Postgres to use function result in WHERE clause without calling it twice? Thanks!

1
  • WHERE cannot use values calculated in the same level of select because it is evaluated before you get values. On the other hand ORDER BY can because it is evaluated after all values are selected. But encapsulate function into subselect in FROM clause and it will work. Commented Nov 23, 2017 at 12:54

3 Answers 3

6

To avoid calling distance_between_objects twice:

--Subquery
SELECT * FROM (
    SELECT 
        *, 
        distance_between_objects(1, id, 7, 3) AS dist 
    FROM 
        clinics) AS clinics_dist 
WHERE 
    dist <= 1;

--CTE
WITH clinics_dist AS (
    SELECT 
        *, 
        distance_between_objects(1, id, 7, 3) AS dist 
    FROM 
        clinics
)
SELECT 
    * 
FROM 
    clinics_dist 
WHERE 
    dist <= 1;

CTE is a cleaner approach in my opinion.

Sign up to request clarification or add additional context in comments.

Comments

4

You can also use a LATERAL

SELECT *
FROM clinics,
LATERAL (SELECT distance_between_objects(1, id, 7, 3) AS dist) l
WHERE l.dist <= 1;

1 Comment

That's a typical use-case for LATERAL, I'd go with this especially that CTEs ignore the underlying indices and make a "new" dataset with no optimisation
-1

You can use the function in the where clause:

SELECT clinics.*, distance_between_objects(1, id, 7, 3) AS dist 
FROM clinics 
WHERE distance_between_objects(1, id, 7, 3)<=1;

2 Comments

The goal is not to call function twice. Like in michel.milezzi answer above.
But to not call function twice, you end by doing multiples runs (sub-queeries, sorts and use temporary space) through the database. Sometimes cleaner code is not the more efficient one database wise. Anyway as far I know, putting functions in fields on the left sode of the query is not performant unless you create a function index on it. This works for all solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.