鉴于以下 SQL 返回基于给定城市的机场列表,我将如何进一步增强它以按离我最近的地方对结果进行排序?看起来它应该很简单,但它让我难以理解。
DECLARE @latRange float
DECLARE @LongRange float
DECLARE @LowLatitude float
DECLARE @HighLatitude float
DECLARE @LowLongitude float
DECLARE @HighLongitude float
DECLARE @Radius float = 100
DECLARE @istartlat float
DECLARE @istartlong float
Select @istartlat=Latitude, @istartlong=Longitude from Lookup where PlaceName = '"Franklin"' and StateCode = '"AR"'
Select @latRange = @Radius / ((6076 / 5280) * 60)
Select @LongRange = @Radius / (((COS((@istartlat * 3.14592653589 / 180)) * 6076.) / 5280. * 60))
Select @LowLatitude = @istartlat - @latRange
Select @HighLatitude = @istartlat + @latRange
Select @LowLongitude = @istartlong - @LongRange
Select @HighLongitude = @istartlong + @LongRange
Select a.City, a.State, a.AirportCode, a.AirportName, a.CountryCode
from PFM_Airport a
where (a.Latitude <= @HighLatitude) and (a.Latitude >= @LowLatitude) and (a.Longitude >= @LowLongitude)
and (a.Longitude <= @HighLongitude)
--and a.CountryCode in ('USA', 'CANADA')
order by a.Latitude, a.Longitude;