如何在SQL中查询某些内容并从子查询中排除结果?

时间:2013-08-20 19:38:10

标签: sql sql-server sql-server-2008 tsql latitude-longitude

我目前有两个问题交给我。第一个在某个地址的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

这是第二个查询(洛杉矶半径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 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

只要参数数量相同,请使用EXCEPT。

Select column from table1
  Except 
    Select column from table2

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

答案 2 :(得分: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

答案 3 :(得分: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