Calculations in a query
Posted on 2006-11-12
I need to write an SQL query that will calculate the distance between to sets of latitude and longitudinal points, and the sorts then by distance.
The point of this is to search for businesses by proximity to a customer's zip code. I have a list of the lat and long values of each zip code the US, and plan to use a LEFT JOIN to join that to the list of companies so that a user can search just by using their zip code.
Here is the equation I will be using:
d = arc(sin(p1.lat)*sin(p2.lat) + cos(p1.lat)* cos(p2.lat)* cos(p2.lon-p1.lon)) * R; where R = 6371, and represents the radius of the earth in kilometers.