2016-09-28 18 views
0

これはクローンの質問かもしれませんが、私が検索した他の回答の中には意味がありません。私はまだSQLを勉強していますので、あなたが私にこのことを指導してもらえたらうれしいです。前もって感謝します。SQL距離計算と1点間

私はこのテーブルを持っています(それ以上のデータがあります)、私はフィウミチーノ空港から最も遠い空港の名前を取得する必要があります(これは私が唯一の経度と緯度データ)と私は距離関数でそれを行う必要があります。

+0

使用しているデータベースのタイプは何ですか? – anakic

+0

あなたが試したことを私たちに見せて、あなたの問題を解決できなかった理由を説明できますか? – dfundako

+0

互換性のないデータベースタグを削除しました。使用しているデータベース用のものを追加し直します。 –

答えて

0

Sql tableあなたは(数千マイル以上の何のための短い距離のためのシンプルな直線ピタゴラス、または大円式)を使用しているどのような距離の関数、

Select * from table 
where [DistanceFunction] 
     (Latitude, Longitude, FiumicinoLatitude, FiumicinoLongitude) = 
    (Select Max([DistanceFunction] 
     (Latitude, Longitude, FiumicinoLatitude, FiumicinoLongitude)) 
     From table) 

あなたは最も離れた空港を検索する必要がある場合いくつかの任意空港(常にではないフィウミチーノ)から、その後、@codeと仮定すると、任意の空港の空港コードです:

Select * from table t 
    join table r on r.code = @code 
where [DistanceFunction] 
     (t.Latitude, t.Longitude, r.Latitude, r.Longitude) = 
    (Select Max([DistanceFunction] 
     (Latitude, Longitude, r.Latitude, r.Longitude)) 
3

単にSQLクエリ

01次実行することができます

SELECT *, (3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM table_name;

ここで、

、キロの代わりに、マイルによる距離を検索6371.

で3959を交換してください入力緯度

-122は、あなたの入力は経度

緯度テーブルですされます空港の緯度の値を含む列名

LNG

詳細は答える空港経度値を含むテーブルのカラム名です:Creating a store locator

0

あなたは各空港から遠いものを見つけようとしている場合は、機能が必要になります

のSQL Server 。しかし、あなたがFCOと言って以来、私はFCOのためにしました。

--temp table for testing 

select 'FCO' as code, 'Fiumicino' as name, 'Rome' as city, 'Italy' as country, 41.7851 as latitude, 12.8903 as longitude into #airports 
union all 
select 'VCE', 'Marco Polo','Venice','Italy',45.5048,12.3396 
union all 
select 'NAP', 'capodichino','Naples','Italy',40.8830,14.2866 
union all 
select 'CDG', 'Charles de Gaulle','Paris','France',49.0097,2.5479 

--create a point from your LAT/LON 
with cte as(
select 
    *, 
    geography::Point(latitude,longitude,4326) as Point --WGS 84 datum 
from #airports), 

--Get the distance from your airport of interest and all others. 
cteDistance as(
select 
    *, 
    Point.STDistance((select Point from cte where code = 'FCO')) as MetersToFiuminico 
from cte) 

--this is the one that's furthest away. Remove the inner join to see them all 
select d.* 
from 
    cteDistance d 
    inner join(select max(MetersToFiuminico) as m from cteDistance where MetersToFiuminico > 0) d2 on d.MetersToFiuminico = d2.m 
関連する問題