我目前有两个问题交给我。第一个搜索我们系统中某个地址半径 50 英里内的联系人。第二个查询做同样的事情,只是它搜索 100 英里半径。
我需要做的是修改第二个查询,使其排除第一个查询的结果。如果你能想象它,我们基本上是在创建一个 50 英里厚的甜甜圈形状的区域。
这是第一个查询(洛杉矶半径 50 英里):
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (order by t0.name ASC, t0.accountid) AS RowNumber, t0.accountid as pkt0
, t0.name as cn
, t0.name as c1
, t1.name as c2
, t1.homephone as c3
, t1.mobilephone as c4
, t1.officephone as c5
, t1.contactid as pkt1
FROM [account] as t0
Left Join [contact] as t1
ON t0.primarycontactid = t1.contactid
WHERE (((
((t0.shippingaddress not like '') AND (t0.shippingaddresslatitude >= 33.6907920399124 ) AND (t0.shippingaddresslatitude <= 34.4136759600876 ) AND (t0.shippingaddresslongitude >= -118.679928573928 ) AND (t0.shippingaddresslongitude <= -117.807441426072 ))
)) AND (t0.deleted = 0))
) _tmpInlineView
WHERE RowNumber > 0
这是第二个查询(LA 100 英里半径):
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (order by t0.name ASC, t0.accountid) AS RowNumber, t0.accountid as pkt0
, t0.name as cn
, t0.name as c1
, t1.name as c2
, t1.homephone as c3
, t1.mobilephone as c4
, t1.officephone as c5
, t1.contactid as pkt1
FROM [account] as t0
Left Join [contact] as t1
ON t0.primarycontactid = t1.contactid
WHERE (((
((t0.shippingaddress not like '') AND (t0.shippingaddresslatitude >= 33.3293500798248 ) AND (t0.shippingaddresslatitude <= 34.7751179201752 ) AND (t0.shippingaddresslongitude >= -119.11615629035 ) AND (t0.shippingaddresslongitude <= -117.37121370965 ))
)) AND (t0.deleted = 0))
) _tmpInlineView
WHERE RowNumber > 0
第二个查询是正确的,只是它必须排除第一个查询的结果。我敢肯定这可能很简单,但我多年来没有手动编写任何 SQL。