sql >> Database teknologi >  >> RDS >> Sqlserver

Returner Unikt sæt med DISTINCT og flere andre valgte kolonner

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. Left Join udkonkurrerer Inner Join?

  2. MySQL - SELECT WHERE field IN (underforespørgsel) - Ekstremt langsom hvorfor?

  3. Tving brugerne til at have den seneste version af Android-appen

  4. fejl ved indsættelse i tabel med i stedet for trigger fra entity data framework