返回带有“DISTINCT”和其他多个Selected列的唯一集

时间:2011-04-25 15:51:32

标签: sql-server-2005 tsql

我正在从多个表创建一个视图。包含的每个表都有一个名为Contact_ID的列。我正在使用MS SQL 2005.我看到我可以使用DISTINCT返回Contact_ID的唯一集,但我的视图只显示Contact_ID。

我需要视图不仅包含Contact_ID,所以我需要创建一个包含所有正确列的SELECT语句,但我不能有重复的记录。

以下是我目前所拥有的内容:(这将返回我需要的所有数据,但包含重复的Contact_ID记录)

SELECT dbo.[1_MAIN - Contacts].Contact_ID, dbo.[1_MAIN - Contacts].Date_entered_into_Database, dbo.[1_MAIN - Contacts].Date_of_Initial_Contact, 
         dbo.[1_MAIN - Contacts].[Company_ Name], dbo.[1_MAIN - Contacts].Key_Contact_Title, dbo.[1_MAIN - Contacts].Key_Contact_First_Name, 
         dbo.[1_MAIN - Contacts].Key_Contact_Middle, dbo.[1_MAIN - Contacts].Key_Contact_Last_Name, dbo.[1_MAIN - Contacts].Key_Credential, 
         dbo.[1_MAIN - Contacts].Key_Contact_Occupation, dbo.[1_MAIN - Contacts].Key_Degree_1, dbo.[1_MAIN - Contacts].Key_Degree_2, 
         dbo.[1_MAIN - Contacts].Key_Degree_3, dbo.[1_MAIN - Contacts].Date_of_Highest_Degree, dbo.[1_MAIN - Contacts].Work_Setting, 
         dbo.[1_MAIN - Contacts].Website_Address, dbo.[1_MAIN - Contacts].Email_1_Key_Contact, dbo.[1_MAIN - Contacts].Email_2, 
         dbo.[1_MAIN - Contacts].Email_3, dbo.[1_MAIN - Contacts].Day_Time_Phone_Number, dbo.[1_MAIN - Contacts].Extension, 
         dbo.[1_MAIN - Contacts].Mobile_Phone_Number, dbo.[1_MAIN - Contacts].Bus_Fax_Number, dbo.[1_MAIN - Contacts].Home_Phone_Number, 
         dbo.[1_MAIN - Contacts].Home_Fax_Number, dbo.[1_MAIN - Contacts].Mailing_Street_1, dbo.[1_MAIN - Contacts].Mailing_Street_2, 
         dbo.[1_MAIN - Contacts].Mailing_City, dbo.[1_MAIN - Contacts].Mailing_State, dbo.[1_MAIN - Contacts].[Mailing_Zip/Postal], 
         dbo.[1_MAIN - Contacts].Mailing_Country, dbo.[1_MAIN - Contacts].[Bad_Address?], dbo.[1_MAIN - Contacts].[PROV/REG?], 
         dbo.[1_MAIN - Contacts].status_flag, dbo.[1_MAIN - Contacts].status_flag AS status_flag2, dbo.Providers.Contact_ID AS Expr1, 
         dbo.Providers.Referral_Source, dbo.Resource_Center.access, dbo.Referral.Contact_Source, dbo.Resource_Center.cert_start_date, 
         dbo.Resource_Center.cert_exp_date, dbo.prov_training_records.Contact_ID AS Expr2, dbo.prov_training_records.date_reg_email_sent
FROM   dbo.[1_MAIN - Contacts] INNER JOIN
         dbo.Referral ON dbo.[1_MAIN - Contacts].Contact_ID = dbo.Referral.Referral_ID INNER JOIN
         dbo.prov_training_records ON dbo.[1_MAIN - Contacts].Contact_ID = dbo.prov_training_records.Contact_ID LEFT OUTER JOIN
         dbo.Resource_Center ON dbo.[1_MAIN - Contacts].Contact_ID = dbo.Resource_Center.Contact_ID FULL OUTER JOIN
         dbo.Providers ON dbo.[1_MAIN - Contacts].Contact_ID = dbo.Providers.Contact_ID

表名别名的相同查询:

SELECT c.Contact_ID, c.Date_entered_into_Database, c.Date_of_Initial_Contact, 
       c.[Company_ Name], c.Key_Contact_Title, c.Key_Contact_First_Name, 
       c.Key_Contact_Middle, c.Key_Contact_Last_Name, c.Key_Credential, 
       c.Key_Contact_Occupation, c.Key_Degree_1, c.Key_Degree_2, 
       c.Key_Degree_3, c.Date_of_Highest_Degree, c.Work_Setting, 
       c.Website_Address, c.Email_1_Key_Contact, c.Email_2, 
       c.Email_3, c.Day_Time_Phone_Number, c.Extension, 
       c.Mobile_Phone_Number, c.Bus_Fax_Number, c.Home_Phone_Number, 
       c.Home_Fax_Number, c.Mailing_Street_1, c.Mailing_Street_2, 
       c.Mailing_City, c.Mailing_State, c.[Mailing_Zip/Postal], 
       c.Mailing_Country, c.[Bad_Address?], c.[PROV/REG?], 
       c.status_flag, c.status_flag AS status_flag2, p.Contact_ID AS Expr1, 
       p.Referral_Source, rc.access, r.Contact_Source, rc.cert_start_date, 
       rc.cert_exp_date, tr.Contact_ID AS Expr2, tr.date_reg_email_sent
FROM   dbo.[1_MAIN - Contacts] c INNER JOIN
       dbo.Referral r ON c.Contact_ID = r.Referral_ID INNER JOIN
       dbo.prov_training_records tr ON c.Contact_ID = tr.Contact_ID LEFT OUTER JOIN
       dbo.Resource_Center rc ON c.Contact_ID = rc.Contact_ID FULL OUTER JOIN
       dbo.Providers p ON c.Contact_ID = p.Contact_ID

2 个答案:

答案 0 :(得分:2)

WITH    q AS
        (
        SELECT  *,
                ROW_NUMBER() OVER (PARTITION BY dbo.[1_MAIN - Contacts].Contact_ID ORDER BY dbo.[1_MAIN - Contacts].Contact_ID) AS rn
        FROM    dbo.[1_MAIN - Contacts]
        INNER JOIN
                dbo.Referral
        ON      dbo.[1_MAIN - Contacts].Contact_ID = dbo.Referral.Referral_ID
        INNER JOIN
                dbo.prov_training_records
        ON      dbo.[1_MAIN - Contacts].Contact_ID = dbo.prov_training_records.Contact_ID
        LEFT OUTER JOIN
                dbo.Resource_Center
        ON      dbo.[1_MAIN - Contacts].Contact_ID = dbo.Resource_Center.Contact_ID
        FULL OUTER JOIN
                dbo.Providers
        ON      dbo.[1_MAIN - Contacts].Contact_ID = dbo.Providers.Contact_ID
        )
SELECT  *
FROM    q
WHERE   rn = 1

答案 1 :(得分:1)

最初误读了这个问题,你需要对所有数据进行分组,但是如果你想显示所有数据并且有独特的数据,你需要在某些情况下使用min或max,如果有人有2部手机......那么你想表演吗?