条件为 sql 的 Join 语句

Join statement with condition in sql

a.geosid    a.Latitude   a.Longitude    b.Latitude  b.Longitude     b.geosid
9589565     -36.013472  -71.426018      -36.0135    -71.426         9589565
9586071     -36.015     -71.498         -36.1104    -71.4416        9586071
9589565     -36.013473  -71.426017      -36.0135    -71.426         9589565

上面的数据是由 运行 在 sql 中的查询形成的,类似于

SELECT *
FROM   [ChileCSVimports].[dbo].[tLocation] a
       JOIN AIRGeography..tGeography b
         ON a.GeographySID = b.GeographySID 

我需要 select 数据,这样两个不同表的两个经纬度如果相差 0.0002(分别)或更多,则不要 select 数据 --

您可以添加到您的 JOIN 条件或使用 WHERE 条件来比较值:

SELECT *
FROM   [ChileCSVimports].[dbo].[tLocation] a
       JOIN AIRGeography..tGeography b
         ON a.GeographySID = b.GeographySID 
        AND ABS(a.Latitude - b.Latitude) < 0.0002 
        AND ABS(a.Longitude - b.Longitude) < 0.0002

使用 ABS() 到 return 的绝对值,这样您就不必担心哪个值比另一个大。

where 子句中添加过滤器。试试这个。

SELECT *
FROM   [ChileCSVimports].[dbo].[tLocation] a
       JOIN AIRGeography..tGeography b
         ON a.GeographySID = b.GeographySID 
Where ABS(a.Latitude - b.Latitude) < 0.0002  
and   ABS(a.Longitude - b.Longitude) <0.0002

听起来很简单,因为它们都是数字。由于您使用的是内部联接,因此您可以将条件放入联接中:

SELECT *
FROM   [ChileCSVimports].[dbo].[tLocation] a
       JOIN AIRGeography..tGeography b
         ON a.GeographySID = b.GeographySID
         AND ABS(a.Latitude - b.Latitude) < 0.0002
         AND ABS(a.Longitude - b.Longitude) < 0.0002

... 或在单独的 WHERE 子句中:

SELECT *
FROM   [ChileCSVimports].[dbo].[tLocation] a
       JOIN AIRGeography..tGeography b
         ON a.GeographySID = b.GeographySID
WHERE    ABS(a.Latitude - b.Latitude) < 0.0002
         AND ABS(a.Longitude - b.Longitude) < 0.0002

无论哪种方式,只需让数据库服务器在检索结果集时进行计算即可。这个 ABS() 函数在大多数具有相同语法的 DBMS 上可用; PostgreSQL 使用“@”运算符作为 shorthand(@ -5.4 的计算结果为 5.4)。