2013-11-25 67 views
0

我在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

我還沒有創建一個表結構來驗證這一點,但像下面可能是使用:

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的內置支持georgraphy。

你可以閱讀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