Returning results within Latitude / Longitude rect

2019-09-21 05:52发布

问题:

Here is an example of my table:

CREATE TABLE [dbo].[Addresses] (
[Id] int NOT NULL IDENTITY(1,1) ,
[Latitude] float(53) NULL,
[Longitude] float(53) NULL
)

From our application I am getting back 2 sets of Latitude and Logitude Points in a bounding box format:

{"upperright":[32.91052662576775,-79.5290690551758],"lowerleft":[32.6420709033305,-80.33313094482423]}

So, based on what comes back in the bounding box results, I need to pass that information to an SQL query to find all of the addresses that are within that rectangle.

回答1:

To get The Duplicate Latitude and Longitude

WITH Dup_LatLong AS
(
SELECT row_number() OVER (PARTITION BY
LATITUDE, LONGITUDE ORDER BY Rpropid1) AS RowNumber, UniqueValue, convert(Varchar(50),LATITUDE)+convert(Varchar(50),LONGITUDE) Check_Cond
FROM YourTable)
Select UniqueValue,LATITUDE, LONGITUDE, convert(Varchar(50),LATITUDE)+convert(Varchar(50),LONGITUDE)
from YourTable
where convert(Varchar(50),LATITUDE)+convert(Varchar(50),LONGITUDE) in (Select Check_Cond from Dup_LatLong where RowNumber > 1)
order by 7


回答2:

This worked for me:

Select * from Addresses
Where Addresses.Longitude between -80.33313094482423 and  -79.5290690551758
And Addresses.Latitude between 32.6420709033305 and 32.91052662576775