Skip to content
Advertisement

Finding shortest geospatial distance in SQL

I’m trying to find the shortest distance between the places and the user’s location and I have two table namely: Users_Location and Places

User’s Table:

Places Table

I want to find the shorted distance between user’s latitude-longitude to all other places.

The final table would look like:

I’m using Snowflake as a data-warehouse and tried to use the HAVERSINE function to calculate the geospatial distance. But I don’t get how do I loop over each place in Places table to get the shortest distance using SQL

Advertisement

Answer

This should out put the closest city to user:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement