问题
I'm using SQL Server.
I have two tables like this:
Table1:
Column1, Column2, Column3, GeoLoc
-----------------------------------
a b c 0xE61...
Table2:
Column1, Column2, Column3, GeoLoc
-----------------------------------
a b c 0xE62...
I am looking to get an output table which will compare all of the points in both tables, and show me where table1
has a GeoLoc
which is within X distance of GeoLoc
in table2
.
Does anyone know how to go about this? One table has around 800 rows, and the other has about 300,000 rows. I'm stumped as to even where to start...
回答1:
Assuming your GeoLoc column is of the 'Geography' data type in SQL server, you should be able to use something like this:
select
t1.*,
t2.*,
t1.GeoLoc.STDistance(t2.GeoLoc) as DistanceApart
from table1 t1
join table2 t2
on (t1.GeoLoc.STDistance(t2.GeoLoc) <= @distanceX)
with the 'DistanceApart' and 'distanceX' values being in meters
来源:https://stackoverflow.com/questions/13747500/calculating-distances-between-two-tables-full-of-geography-points