Invalid column name on Having in SQL Server - sql

I have quick question why I can't use having keyword on distance? I need somehow to check is distance < 20 for example
SELECT
Id, Lat, Lng,
(6367 * acos( cos( radians(45.444) )
* cos( radians( Lat ) ) * cos( radians( Lng ) - radians(158.554) )
+ sin( radians(4545) ) * sin( radians( Lat ) ) ) ) AS distance
FROM
Posts
HAVING
distance < 15 // Invalid column Name
ORDER BY
distance

I might suggest outer apply for this purpose:
SELECT p.Id, p.Lat, p.Lng, d.distance
FROM Posts p OUTER APPLY
(SELECT (6367 * acos( cos( radians(45.444) )
* cos( radians( p.Lat ) ) * cos( radians( p.Lng ) - radians(158.554) )
+ sin( radians(4545) ) * sin( radians( p.Lat ) ) ) ) AS distance
) d
FROM Posts p
WHERE d.distance < 15
ORDER BY distance;
The use of HAVING as a substitute for WHERE is an extension for MySQL. In other databases, you can use a subquery, CTE, or lateral join (which is the technical name for what APPLY does). In this case, I think the lateral join is convenient, because it separates the logic for this quite complicated formula.

Try this
SELECT *
FROM
(SELECT
Id, Lat, Lng,
(6367 * acos(cos(radians(45.444)) * cos(radians(Lat)) *
cos(radians(Lng) - radians(158.554)) + sin(radians(4545)) *
sin(radians(Lat)))) AS distance
FROM Posts) p
WHERE
p.distance < 15
ORDER BY
p.distance

YOu can use brackets to dictate you mean a field name not a pereserved keyword:
SELECT Id,Lat,Lng,(6367 * acos( cos( radians(45.444) )
* cos( radians( Lat ) ) * cos( radians( Lng ) - radians(158.554) )
+ sin( radians(4545) ) * sin( radians( Lat ) ) ) ) AS distance FROM Posts
HAVING [distance] < 15 // Invalid column Name
ORDER BY distance

Related

column "distance" does not exist

I am trying to run a query that finds the places near my location, but every time I try to run it, it returns the following error:
column "distance" does not exist
If I remove distance and only leave up to the FROM posts, it returns the post id and the distance column.
But if I leave it in the original way, it returns the error.
SELECT id,
( 3959 * acos( cos( radians(-32.63) ) * cos( radians( latitude ) ) *
cos( radians( longitude ) - radians(-71.42) ) + sin( radians(-32.63) ) *
sin( radians( latitude ) ) ) ) AS distance
FROM posts
HAVING distance < 25
ORDER BY distance;
You can't use a column alias in other places in the same query. You'll have to either repeat the entire expression in your HAVING and ORDER BY or use your existing query as a sub-query and apply the HAVING and ORDER BY to the outer query, or use a CTE if your RDBMS supports it.
SELECT id, distance FROM
(SELECT id, ( 3959 * acos( cos( radians(-32.63) ) * cos( radians( latitude ) ) *
cos( radians( longitude ) - radians(-71.42) ) + sin( radians(-32.63) ) *
sin( radians( latitude ) ) ) ) AS distance FROM posts) p
WHERE distance < 25 ORDER BY distance;
you need to put CTA to order it by distance

Sequel: PG::UndefinedColumn: ERROR: column "distance" does not exist

I'm trying to get a list of businesses nearby using this query:
query = "SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM businesses HAVING distance < ? ORDER BY distance"
After running this:
records = Api.DB.fetch(query, lng, lat, distance)
I get that PG::UndefinedColumn: ERROR: column "distance" does not exist.
distance isn't a column in the table but I created it as a calculated column in the query so it suppose to work but it's not?
Any ideas?
Postgres supports lateral joins, so you can do this without a subquery or CTE as:
SELECT b.id, v.distance
FROM business b CROSS JOIN LATERAL
(VALUES ( 3959 * acos( cos( radians(37) ) * cos( radians( b.lat ) ) * cos( radians( b.lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( b.lat ) ) )
)
) v(distance)
WHERE v.distance < ?
ORDER BY distance;
The query you are using was poached from MySQL, where select aliases can be used in a HAVING clause. But in Postgres, you will have to either subquery or just repeat the distance formula. Using the latter option as an example, here is one viable version of your Haversine query:
SELECT
id,
( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance
FROM business
WHERE
( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) )
< ?
ORDER BY
distance;

SQL: Average from result set within a radius (Latitude / Longitude)

I'm trying to get the total average price within a radius by given latitude and longitude.
I've tried something like this:
SELECT avg(price) AS average,
( 6371 * acos( cos( radians(37.3541079) ) * cos( radians( ANY_VALUE(`latitude` )) ) * cos( radians( ANY_VALUE(`longitude`) ) - radians(-121.9552356) ) + sin( radians(37.3541079) ) * sin( radians( ANY_VALUE(`latitude`) ) ) ) ) AS distance
FROM `Rental`
HAVING distance <= 20
But the result is empty.
The result should looks like this:
average = 185
Thanks for your help.
You are using the HAVING in a way that MySQL extends it. But because you have AVG(), it places its normal role as well.
You are going to need a subquery or to move the distance measurement to the WHERE clause:
SELECT avg(price) as average
FROM (SELECT r.*,
( 6371 * acos( cos( radians(37.3541079) ) * cos( radians( ANY_VALUE(`latitude` )) ) * cos( radians( ANY_VALUE(`longitude`) ) - radians(-121.9552356) ) + sin( radians(37.3541079) ) * sin( radians( ANY_VALUE(`latitude`) ) ) ) ) AS distance
FROM `Rental` r
) r
WHERE distance <= 20;
In other words, calculate the distance. Then use WHERE to filter the rows. And then aggregate for the price.

SQL query of Haversine formula in SQL server

I am trying to get drivers who visited certain area from SQL server 2014 database. The table is named DriverLocationHistory.
Here is the sql query I used :
SELECT id, ( 6371 * acos( cos( radians(37) ) * cos( radians( latitude ) )
* cos( radians( Longitude ) - radians(-122) ) + sin( radians(37) ) * sin(radians(latitude)) ) ) AS distance
FROM DriverLocationHistory
HAVING distance < 5
ORDER BY distance
When I execute the query I get this error :
Msg 207, Level 16, State 1, Line 7
Invalid column name 'distance'.
I was asked to provide a way to do this with the built-in geography data type. That's a bit too long for a comment, so here goes:
ALTER TABLE [DriverLocationHistory] ADD geoLocation
AS geography::Point([Latitude], [Longitude], 4236);
declare #p geography = geography::Point(37, -122, 4236);
select * from [DriverLocationHistory]
where geoLocation.STDistance(#p) < 5000;
A spatial index should help with the SARGability of the where clause in the select.
You can't use an alias in a Where clause. This is because the where clause is processed before, or as, the result set is being generated, and the alias is not assigned until the result set has been generated. Only in an aggregate query (where there is a group By) can you do this, because then the alias is assigned to the value of some expression before the aggregation is processed. Your query must use the full expression in both the where clause (does not need to be a Having clause) and in the order by:
SELECT id,
( 6371 * acos( cos( radians(37) )
* cos( radians( latitude ) )
* cos( radians( Longitude ) - radians(-122) ) + sin( radians(37) )
* sin(radians(latitude)) ) ) AS distance
FROM DriverLocationHistory
Where 6371 * acos( cos( radians(37) )
* cos( radians( latitude ) )
* cos( radians( Longitude ) - radians(-122) ) + sin( radians(37) )
* sin(radians(latitude)) ) < 5
ORDER BY 6371 * acos( cos( radians(37) )
* cos( radians( latitude ) )
* cos( radians( Longitude ) - radians(-122) ) + sin( radians(37) )
* sin(radians(latitude)) )
as mentioned in comments, you can use the alias in the order by,
Or, you could also perform the computation and alias assignment in a subquery:
SELECT id, distance
From (Select ( 6371 * acos( cos( radians(37) )
* cos( radians( latitude ) )
* cos( radians( Longitude ) - radians(-122) ) +
sin( radians(37) )
* sin(radians(latitude)) ) ) distance
From DriverLocationHistory)z
Where distance < 5
ORDER BY distance

Having/Where clause not working for Haversine Formula using Microsoft SQL

Here is my table named "test" -
Check the snapshot here test
And then my row items: here - rowitems
I'm using the haversine formula, he is my query
SELECT *, ( 3960 * acos( cos( radians( 33.650800 ) ) *
cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians( -117.891729 ) ) +
sin( radians( 33.650800 ) ) * sin( radians( Latitude ) ) ) ) AS Distance
FROM test
For some reason the HAVING or WHERE clause is not working with Distance.
It works with Latitude or Longitude.
But when I try to do WHERE Distance < 10 or HAVING Distance < 10. It says Distance is an invalid column name.
I need to be able to do this, and make a query using Distance. Any help would be appreciated.
You cannot use calculated fields on a where or having clause. Create a view or use a subquery
Try this:
select * FROM (SELECT *, ( 3960 * acos( cos( radians( 33.650800 ) ) *
cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians( -117.891729 ) ) +
sin( radians( 33.650800 ) ) * sin( radians( Latitude ) ) ) ) AS Distance
FROM test) as T WHERE T.Distance < 10
You need to put your query into a subquery, or a view, or a CTE (common table expression).
Here is an example for your task with a CTE:
WITH cte_test (Name, Latitude, Longitude, Distance)
AS
(
SELECT Name, Latitude, Longitude,
3960 * acos(cos(radians(33.650800))
* cos(radians( Latitude ) )
* cos( radians( Longitude ) - radians( -117.891729 ) )
+ sin( radians( 33.650800 ) ) * sin( radians( Latitude ) ) ) )
AS Distance
FROM test
)
SELECT * from cte_test where Distance < 10 ;
A CTE is a kind of "temporary view". It is also a powerful instrument which can also be used for creating recursive queries.