我在SQL Server数据库中有一个包含列的表:
ID, Latitude, Longitude
我想计算每个点之间的距离并采取所有较小的点,但是当我应用WHERE distance < 10
时,我收到错误
发生了无效的浮点运算
我不确定我做错了什么,因为它在没有WHERE
子句的情况下完美运行。
我的代码:
SELECT
[t5].Name as Hotel1, [t6].Name as Hotel2, Distance
FROM
(SELECT
[t2].ID as GPS1, [T1].ID as GPS2,
(ACOS(Cos(PI()*[t1].Latitude/180.0) * Cos(PI() * [t1].Longitude/180.0) * Cos(PI()*[t2].Latitude/180.0) * Cos(PI() * [t2].Longitude/180.0) + Cos(PI()*[t1].Latitude/180.0) * Sin(PI() * [t1].Longitude/180.0) * Cos(PI()*[t2].Latitude/180.0) * Sin(PI() *[t2].Longitude/180.0) + Sin(PI()*[t1].Latitude/180.0) * Sin(PI() * [t2].[Latitude]/180.0)) * 6371) AS Distance
FROM
GPSLocations [t1]
JOIN
GPSLocations [t2] ON [t1].ID <> [t2].ID) [t4]
JOIN
Hotels [t5] ON [t5].FK_GPSLocationID = GPS1
JOIN
Hotels [t6] ON [t6].FK_GPSLocationID = GPS2
WHERE
distance < 10
示例数据:
GPSLocations表格如下所示
Latitude Longitude ID
39,7531224 -105,0001446 847
55,10309 12,31064 581
55,10317 12,37527 684
55,10382 9,35923 740
55,1097 12,50471 636
55,11163 10,77026 358
55,11366 11,74766 668
酒店表格如下:
ID Name FK_GPSLocationID
64 Hotel Findus 59
65 Best Western CPH 60
66 Comwell Middelfart 61
67 Hotel Middelfart 62
68 Master demo 1 63
69 Sky hotel 1 64
70 Rene bigstart hotel 3 65
这只是示例数据而且外键不匹配。
所有GPS位置均可在此处下载:http://noxiaz.dk/GPSLocations.txt
与表Hotels
的联接对于获取错误