21

mysqlを使用してこの質問に対する回答をたくさん見つけましたが、mssql2008が使用できるクエリに何も変換できませんでした。データベースの各行に経度と緯度の列があります。ユーザーがいる場所の緯度と経度を設定します。ユーザーの緯度/経度からxマイル以内にあるすべての行を検索できるようにしたい。また、SOで見つけた他のクエリを使用しようとすると、エラーが発生し続けます 。'pow' is not a recognized built-in function name. これは、SQL 2008で以前に使用したことがあると確信しているため、奇妙なことですpow。これに関するサポートをいただければ幸いです。これまでのところ、これが最も近いものです。

select * from tbl_MyTable
WHERE (
POW( ( 69.1 * ( Longitude - @longitude ) * cos( @latitude / 57.3 ) ) , 2 ) + POW( ( 69.1 * ( Latitude - @latitude ) ) , 2 )
) < ( 5 *5 );
4

3 に答える 3

41

SQL 2008を使用しているため、ネイティブの地理空間機能の使用を検討してください。あなたは次のような派手なことをすることができます:

  • ポイントを表す地理タイプの永続化された計算列を作成します。
  • 計算列に空間インデックスを作成します。yourPoint.STDistance(@otherPoint) <= @distanceこれは効率的なもののようなものになります

そのようです:

alter table [yourTable] add [p] as geography::Point(Latitude, Longitude, 4326) persisted;
create spatial index [yourSpatialIndex] on [yourTable] ([p])

declare @Latitude float = <somevalue>, @Longitude float = <somevalue>;
declare @point geography = geography::Point(@Latitude, @Longitude, 4326);
declare @distance int = <distance in meters>;

select * from [yourTable] where @point.STDistance([p]) <= @distance;
于 2013-03-26T15:07:11.960 に答える
6
DECLARE @CurrentLocation geography; 
SET @CurrentLocation  = geography::Point(12.822222, 80.222222, 4326)

SELECT * , Round (GeoLocation.STDistance(@CurrentLocation ),0) AS Distance FROM [Landmark]
WHERE GeoLocation.STDistance(@CurrentLocation )<= 2000 -- 2 Km

素晴らしいチュートリアル

http://www.sql-server-helper.com/sql-server-2008/convert-latitude-longitude-to-geography-point.aspx

于 2014-10-21T07:24:16.557 に答える
5

捕虜ではなく力が欲しいと思います

http://msdn.microsoft.com/en-us/library/ms174276.aspx

于 2013-03-26T03:28:27.620 に答える