Hi @Jankowski, Mark ,
Welcome to Microsoft Q&A!
It is recommended to provide your sample data of your table 'WED_GPS$' and your code instead of snapshots so that we could copy and paste into a query window to develop a tested query quickly.
Please refer below one simple example and check whether it is helpful to you.
create table #temp2
(mobile varchar(20),
LATITUDE float,
LONGITUDE float)
insert into #temp2 values
('SCBBP62_123212',27.8950996398925,-82.7725982666015),
('SCBBP63_126719',27.8950996398925,-82.7725982666015),
('SCBBP65_123211',27.8952007293701,-82.7726974487304),
('SCBBP66_124694',27.9362007141113,-82.8116989135742)
;WITH X AS
(
SELECT *,
geography::Point(28.0936, -82.7643, 4326) SOURCE
,geography::Point(LATITUDE, LONGITUDE, 4326) DESTINATION
FROM #temp2
)
SELECT top 3 mobile,LATITUDE,LONGITUDE,
SOURCE.STDistance(DESTINATION)/1609.344 AS MILES
FROM X
ORDER BY SOURCE.STDistance(DESTINATION)/1609.344
Output:
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.