我的查询仅匹配彼此相同的邮政编码。我需要一个查询,可以将半径为20英里的所有邮政编码返回给定的邮政编码。
DECLARE @cZip VARCHAR(5)
SET @cZip = '63026'
DECLARE @dMin DECIMAL = 20 * 32186.9 -- metres
DECLARE @c GEOGRAPHY
SELECT @c = GEOGRAPHY::Parse('Point(' + CAST(longitude AS VARCHAR)+ ' ' + CAST(latitude AS VARCHAR) + ')')
FROM wcr_sales_zip_info.dbo.utT_ZIP_Long_Lat
WHERE ZIP_CD = @cZip
SELECT S.ZIP_CD
,AAE.AGN_FIRST_NM
,AAE.AGN_LAST_NM
,AAE.AGN_ADDRESS_CD
,AAE.AGN_CITY_CD
,AAE.AGN_STATE_CD
,AAE.AGN_ZIP_CD
,AAE.AGN_AGENT_NBR
,AAE.AGN_EMAIL_NBR
,AC.CANDIDATE_ID
,AC.FIRST_NM
,AC.LAST_NM
,AC.ADDRESS_TXT
,AC.CITY
,AC.STATE_CD
,AC.ZIP AS 'ZIP1'
,STUFF(STUFF(STUFF(AC.HOME_PHONE_NBR,1,0,' ('),6,0,') '),11,0,'-') AS 'HOME_PHONE_NBR'
,STUFF(STUFF(STUFF(AC.ALT_PHONE_NBR,1,0,' ('),6,0,') '),11,0,'-') AS 'ALT_PHONE_NBR'
,AC.EMAIL
,AC.PREF_CONTACT_METHOD_CD
,AC.TRAVEL_TIME_MINS_NBR
,AC.TRAVEL_DISTANCE_MLS_NBR
,AC.ADDED_DT
FROM wcr_sales_zip_info.dbo.utT_ZIP_Long_Lat S
JOIN ASRD_AGENT_AUTO_EMAIL AAE ON S.ZIP_CD = AAE.AGN_ZIP_CD
JOIN ASRD_CANDIDATES AC ON S.ZIP_CD = AC.ZIP
WHERE --AAE.AGN_ZIP_CD >= '63026'
--AND
@c.STDistance(GEOGRAPHY::Parse('Point(' + CAST(longitude AS VARCHAR) + ' ' + CAST(latitude AS VARCHAR) + ')')) >= @dMin
AND AC.ADDED_DT >= DATEADD(day, -7, GETDATE())
答案 0 :(得分:2)
由于计算的复杂性,我使用UDF。
这里传递基础Lat / Lng以及待测试的Lat / Lng
要明确:距离是#34;因为乌鸦飞得很快"而不是开车距离。
例如:
Declare @BaseZip varchar(10) = '02806'
Declare @Within int = 20
Select Distinct
BaseZip = A.ZipCode
,B.ZipCode
,B.CityName
,B.StateCode
,Miles = [dbo].[udf-Geo-Calc-Miles] (A.Lat,A.Lng,B.Lat,B.Lng)
From (Select Distinct ZipCode,Lat,Lng From [dbo].[ZipCodes] where ZipCode=@BaseZip) A
Join [dbo].[ZipCodes] B
on [dbo].[udf-Geo-Calc-Miles] (A.Lat,A.Lng,B.Lat,B.Lng) <= @Within
Order By 5
返回
UDF如有兴趣
CREATE Function [dbo].[udf-geo-Calc-Miles] (@Lat1 float,@Lng1 float,@Lat2 Float,@Lng2 float)
Returns Float as
Begin
Declare @Miles Float = (Sin(Radians(@Lat1)) * Sin(Radians(@Lat2))) + (Cos(Radians(@Lat1)) * Cos(Radians(@Lat2)) * Cos(Radians(@Lng2) - Radians(@Lng1)))
Return Case When @Miles is null then 0 else abs((3958.75 * Atan(Sqrt(1 - power(@Miles, 2)) / @Miles))) end
End
修改强>
您可能会注意到一些重复/相同的里程。您可能知道,城市可能有其他名称,例如布朗大学和布朗站位于罗德岛普罗维登斯,但组织也可能有自己的邮政编码。