1

我目前有两个问题交给我。第一个搜索我们系统中某个地址半径 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。

4

4 回答 4

3

它更多的是减去给定纬度和经度的表面。
编辑:这不仅仅是减法,因为有两个“外部”部分 - 逻辑在 100 英里逻辑中被 AND NOT (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 
/* 100 miles */
(t0.shippingaddresslatitude >= 33.3293500798248 AND t0.shippingaddresslatitude <= 34.7751179201752 ) 
/* but not 50 miles */
and not( t0.shippingaddresslatitude >= 33.6907920399124 AND t0.shippingaddresslatitude <= 34.4136759600876 )
/* SAME for LONGITUDE */
AND (t0.shippingaddresslongitude >= -119.11615629035 AND t0.shippingaddresslongitude <= -117.37121370965 )
and not (t0.shippingaddresslongitude >= -118.679928573928 AND t0.shippingaddresslongitude <= -117.807441426072 )


AND (t0.deleted = 0))
) _tmpInlineView
WHERE RowNumber > 0
于 2013-08-20T19:47:48.640 回答
2

只要它们具有相同数量的参数,就使用 EXCEPT。

Select column from table1
  Except 
    Select column from table2

http://technet.microsoft.com/en-us/library/ms188055.aspx

于 2013-08-20T19:40:28.150 回答
1
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
EXCEPT
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
于 2013-08-20T19:45:15.277 回答
1

使用除。您可能需要根据口味对其进行修改。像这样的东西...

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



EXCEPT


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
于 2013-08-20T19:46:13.727 回答