选择仅匹配地址匹配的一行

时间:2014-02-02 21:49:41

标签: sql sql-server

使用SQL Server,我有一个Contact表,其中包含联系人的姓名和地址。我有几个多个联系人住在同一个地址的情况。发送简报时,我只想将其发送到每个地址一次。如何修改下面的查询以仅显示每个地址的第一个联系人?

SELECT 
   dbo_Contact.Contact_Title AS Title, 
   dbo_Contact.Contact_FirstName AS [First Name],
   dbo_Contact.Contact_LastName AS [Last Name], 
   dbo_Contact.Contact_Suffix AS Suffix,
   dbo_Contact.Business_Name AS [Business Name], 
   dbo_Contact.Contact_Address1 AS [Address 1],
   dbo_Contact.Contact_Address2 AS [Address 2], 
   dbo_Contact.Contact_City AS City,
   dbo_Contact.Contact_State AS State, 
   dbo_Contact.Contact_Zip AS Zip,
   dbo_Contact.Contact_Email AS Email
FROM 
   dbo_Contact 
INNER JOIN 
   dbo_Mailing_Subscribers ON dbo_Contact.[ContactID] = dbo_Mailing_Subscribers.[ContactID]
WHERE 
   (((dbo_Contact.Contact_Inactive) = False) 
    AND ((dbo_Mailing_Subscribers.Mailing_ID) = True)
    AND ((dbo_Mailing_Subscribers.Subscribed) = True));

例如,如果Kurt和William居住在123 A Street而Steve居住在123 B Avenue,我只想返回Kurt和Steve的记录。

2 个答案:

答案 0 :(得分:0)

您可以使用row_number()和子查询:

select <list of columns you want>
from (SELECT row_number() over (partition by Contact_Address1, Contact_Address2, Contact_Zip
                                order by ContactId
                               ) as seqnum
       dbo_Contact.Contact_Title AS Title, 
       dbo_Contact.Contact_FirstName AS [First Name],
       dbo_Contact.Contact_LastName AS [Last Name], 
       dbo_Contact.Contact_Suffix AS Suffix,
       dbo_Contact.Business_Name AS [Business Name], 
       dbo_Contact.Contact_Address1 AS [Address 1],
       dbo_Contact.Contact_Address2 AS [Address 2], 
       dbo_Contact.Contact_City AS City,
       dbo_Contact.Contact_State AS State, 
       dbo_Contact.Contact_Zip AS Zip,
       dbo_Contact.Contact_Email AS Email
    FROM dbo_Contact
    INNER JOIN 
       dbo_Mailing_Subscribers ON dbo_Contact.[ContactID] = dbo_Mailing_Subscribers.[ContactID]
    WHERE 
       (((dbo_Contact.Contact_Inactive) = False) 
        AND ((dbo_Mailing_Subscribers.Mailing_ID) = True)
        AND ((dbo_Mailing_Subscribers.Subscribed) = True))
   ) t
where seqnum = 1;

目前还不清楚“同一地址”和“第一次接触”是什么意思。对于地址,查询使用两个地址行和邮政编码。对于“第一次联系”,查询使用联系人表中的最低ID。

答案 1 :(得分:0)

好的,这是我的解决方案。

1 - 使用表别名来减少代码占用空间。

2 - 将true / false更改为1/0以满足TSQL布尔值。

3 - 按地址1,地址2,邮政编码按姓氏,名字命名创建RowNum分区。

4 - 仅从重复项中选择一行。

;
with cteData as
(
SELECT 
   C.Contact_Title AS Title, 
   C.Contact_FirstName AS [First Name],
   C.Contact_LastName AS [Last Name], 
   C.Contact_Suffix AS Suffix,
   C.Business_Name AS [Business Name], 
   C.Contact_Address1 AS [Address 1],
   C.Contact_Address2 AS [Address 2], 
   C.Contact_City AS City,
   C.Contact_State AS State, 
   C.Contact_Zip AS Zip,
   C.Contact_Email AS Email,
   row_number() over (partition by Contact_Address1, Contact_Address2, Contact_Zip 
       order by Contact_LastName, Contact_FirstName) as RowNum
FROM 
   dbo_Contact as C
INNER JOIN 
   dbo_Mailing_Subscribers as S 
ON C.[ContactID] = S.[ContactID]
WHERE 
   C.Contact_Inactive = 0 AND
   S.Mailing_ID = 1 AND
   S.Subscribed = 1
)
select * from cteData where RowNum = 1;

简而言之,一封邮件将发送给具有最后/第一名壁橱的人.A /。