匹配预定义地址

时间:2018-04-23 06:56:41

标签: sql asp.net sql-server tsql ssrs-2012

我有2个表,1。“Address_Table”和2.Coordinate_Table

我希望将表2中的坐标记录与表1中距离最近的地址匹配,范围为50米。如果没有找到,则返回坐标。

Address_Table:

+--------+-------------+-------------+-------+---------+
| ADD_ID |  Longitude  |  Latitude   | Range | Address |
+--------+-------------+-------------+-------+---------+
|      1 | 72.03092000 | 33.28729000 |    50 | abcd    |
+--------+-------------+-------------+-------+---------+

Coordinate_Table:

+--------+-------------+-------------+
| Cor_id |   Co_Lat    |   Co_GLng   |
+--------+-------------+-------------+
|   8450 | 72.03092011 | 33.28729005 |
|   8451 | 73.03099001 | 32.28729901 |
+--------+-------------+-------------+

Results_View:

+--------+-------------+-------------+-------------------------+
| Cor_id |   Co_Lat    |   Co_GLng   |        Address          |
+--------+-------------+-------------+-------------------------+
|   8450 | 72.03092011 | 33.28729005 | abcd                    |
|   8451 | 73.03099001 | 32.28729901 | 73.03099001,32.28729901 |
+--------+-------------+-------------+-------------------------+

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT
    CT.Id,
    CT.Co_Lat,
    CT.Co_GLng,
    [Address] = COALESCE(AD.[Address],cast(CT.Co_Lat as varchar(50))+','+cast(CT.Co_GLng as varchar(50)),'')
    FROM Coordinate_Table CT
       LEFT JOIN Address_Table AD
          ON CT.Co_Lat BETWEEN AD.Longitude+0.00000005 AND AD.Longitude-0.00000005
             AND CT.Co_GLng BETWEEN AD.Latitude+0.00000005 AND AD.Latitude-0.00000005