I am having issue with ALIAS variable "distance" in my PSQL query. (*please ignore that I am using voutecount as distance)
SELECT rentalid, createdDate, votecount AS distance
FROM rental
WHERE longitude=? AND latitude=?
HAVING distance < 25 ORDER BY distance LIMIT 0 OFFSET 30
The error is "distance does not exist" but I have defined distance already so I cannot tell what the issue is.
nested exception is org.postgresql.util.PSQLException: ERROR: column "distance" does not exist
Position: 107] with root cause
3
Answers
@Maimoona Abid Okay I am not getting any error but the actual SQL command I am running is as below and I am getting any empty resultset. I believe it is because the query is actually searching RENTAL Table for longitude and latitude that I give as parameter instead of using the parameter for calculating the distance.
Try this approach, first create a subquery that calculates the distance alias and then filter and order the results in the outer query.
Hope it works 🙂
You get this error because you are using
HAVING
on an aliasdistance
that you defined in theSELECT
clause and this is not supported. So you have to use theWHERE
clause instead ofHAVING
to filter rows based on thedistance
alias;