Given the following tables:
table A (id, latitude, longitude)
table B (id, latitude, longitude)
how do I build an efficient T-SQL query that associates each row in A with the closest row in B?
The ResultSet should contains all the rows in A and associate them with 1 and only 1 element in B. The format that I'm looking for is the following:
(A.id, B.id, distanceAB)
I have a function that calculates the distance given 2 pairs of latitude and longitude. I tried something using order by ... limit 1 and/or rank() over (partition by ...) as rowCount ... where rowCount = 1 but the result is either not really what I need or it takes too long to return.
Am I missing something?
This is one approach that should have deceent performance, but a big caveat is that it might not find any results
What you are basically doing is looking for any B row within roughly a 20 unit radius of A and then sorting it by your function to determine the closest. You can adjust the unit radius as needed. While it is not exact, it should reduce the size of the result set and should give you decent performance results.