SQL Server查询列出重复值

时间:2012-07-26 20:47:14

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

我有一个SQL查询,它从一个小型数据库中提取联系人列表;它拉数据就好了。然而,SQL的问题在于它将多次列出相同的联系人,具体取决于与联系人关联的电子邮件和地址的数量。

它只应该列出一次。这是我写的SQL:

SELECT DISTINCT 
                Contact_Main.contactid, first_name, last_name, p_title,
                prefix, suffix, email, address1, address2, 
                city, state, zip, Contact_Address_Type.addresstype, 
                Contact_Email_Types.emailtype, 
                addressid, emailid, Contact_Address_Type.addresstypeid, 
                Contact_Email_Types.emailtypeid,
                (SELECT COUNT(addressid) 
                       FROM Contact_Address 
                       WHERE contactid = Contact_Main.contactid) as num_addresses,
                (SELECT COUNT(emailid) 
                        FROM Contact_Email 
                        WHERE contactid = Contact_Main.contactid) as num_emails
        FROM Contact_Main
        LEFT JOIN Contact_Email ON Contact_Main.contactid = 
                                                    Contact_Email.contactid
        LEFT JOIN Contact_Email_Types ON Contact_Email_Types.emailtypeid =
                                                    Contact_Email.emailtypeid
        LEFT JOIN Contact_Address ON Contact_Address.contactid = 
                                                    Contact_Main.contactid
        LEFT JOIN Contact_Address_Type ON Contact_Address_Type.addresstypeid  = 
                                                    Contact_Address.addresstypeid 
        ORDER BY last_name, first_name;

我确信这是一个简单的解决方案,我需要将这部分内容作为一个子查询,但出于某种原因,我无法弄明白。

2 个答案:

答案 0 :(得分:0)

这样可行吗?

SELECT DISTINCT 
                cm.contactid
                , first_name
                , last_name
                , p_title
                , prefix
                , suffix
                , email
                , address1
                , address2
                , city
                , state
                , zip
                , cat.addresstype
                , cet.emailtype
                , addressid
                , emailid
                , cat.addresstypeid
                , cet.emailtypeid
                --, (SELECT COUNT(addressid) 
                --     FROM Contact_Address 
                --     WHERE contactid = cm.contactid) as num_addresses
                --, (SELECT COUNT(emailid) 
                --      FROM Contact_Email 
                --      WHERE contactid = cm.contactid) as num_emails
        FROM Contact_Main cm
        LEFT JOIN Contact_Email ce ON cm.contactid = 
                                                    ce.contactid
        LEFT JOIN Contact_Email_Types cet ON cet.emailtypeid =
                                                    ce.emailtypeid
        LEFT JOIN Contact_Address ON ca.contactid = 
                                                    cm.contactid
        LEFT JOIN Contact_Address_Type ON cat.addresstypeid  = 
                                                    ca.addresstypeid 
        ORDER BY cm.last_name, cm.first_name;

答案 1 :(得分:0)

好的,所以当我上班时,我问老板,SQL代码的处理方式是什么。我们都看了一眼,并确定它正在回归所有可能的结果。在使用SQL Access中的模拟后,我们不得不进行LEFT OUTER JOIN,而不仅仅是LEFT JOIN。

    SELECT DISTINCT Contact_Main.contactid, first_name, last_name, p_title,
                    prefix, suffix, email, address1, address2, 
                    city, state, zip, Contact_Address_Type.addresstype, 
                    Contact_Email_Types.emailtype, 
                    addressid, emailid, Contact_Address_Type.addresstypeid, 
                    Contact_Email_Types.emailtypeid,
  (SELECT COUNT(addressid) FROM Contact_Address 
   WHERE contactid = Contact_Main.contactid) as num_addresses,
  (SELECT COUNT(emailid) FROM Contact_Email 
  WHERE contactid = Contact_Main.contactid) as num_emails
  FROM Contact_Main
  LEFT OUTER JOIN Contact_Email 
  ON Contact_Main.contactid = Contact_Email.contactid
  LEFT OUTER JOIN Contact_Email_Types
  ON Contact_Email_Types.emailtypeid = Contact_Email.emailtypeid
  LEFT OUTER JOIN Contact_Address 
  ON Contact_Address.contactid = Contact_Main.contactid
  LEFT OUTER JOIN Contact_Address_Type 
  ON Contact_Address_Type.addresstypeid  = Contact_Address.addresstypeid 
  ORDER BY last_name, first_name;

这就是诀窍。感谢您的所有建议!