Return Unique set with “DISTINCT” and multiple other Selected columns

后端 未结 2 1147
孤城傲影
孤城傲影 2021-01-23 16:23

I am creating a view from multiple tables. Each table being included has a column named Contact_ID. I am using MS SQL 2005. I see that I can use DISTINCT to return a unique set

相关标签:
2条回答
  • 2021-01-23 16:52

    Initially misread the question, you will need to group all the data but if you want to show all the data and there are uniques, you need to use min or max in some cases, if someone has 2 phones...what do you want to show?

    0 讨论(0)
  • 2021-01-23 16:58
    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
    
    0 讨论(0)
提交回复
热议问题