我在SQL中有一个TruckLocation表。
Select top 4 *
from TruckLocation
order by ReadTime desc
结果:
OID TruckID Lat Lng Speed ReadTime
123 33 37.4060 37.1470 58 2013-11-26 00:39:00.000
334 23 40.735 35.8159 90 2013-11-26 00:33:00.000
123 33 37.4060 37.1470 58 2013-11-26 00:31:00.000
334 23 42.735 36.3159 85 2013-11-26 00:27:00.000
我想创建一个新的表或存储过程结果是这样的:
Time TruckID TotalDistance(KM)
2013-11-26 23 125,1245484
2013-11-25 23 123,1245484
2013-11-24 23 43,1245484
2013-11-23 23 434,1245484
2013-11-22 23 56,1245484
2013-11-21 23 85,1245484
. . .
. . .
. 18 1215,4544
. . .
我试过但我失败了。
答案 0 :(得分:0)
我还没有创建一个表结构来验证这一点,但可能会使用以下内容:
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POINT(40.735 35.8159)', 4326);
SET @h = geography::STGeomFromText('POINT(42.735 36.3159)', 4326);
--SELECT @g.STDistance(@h);
SELECT CONVERT(DATETIME, CAST(readtime AS VARCHAR(11)),103), truckid, @g.STDistance(@h)
FROM [trucklocation]
GROUP BY readtime
这使用了SQL Server 2008的内置地理支持。
您可以在此处阅读有关STGeomFromText的信息http://technet.microsoft.com/en-us/library/bb933834(v=sql.100).aspx 你可以在这里阅读STDistance http://technet.microsoft.com/en-us/library/bb933808(v=sql.100).aspx