Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Rufen Sie eindeutige Zeilen aus einem Ergebnis von JOIN in SQL Server ab

Du erhältst Duplikate, weil es mehrere Fotos pro Album gibt. Um eine zu erhalten, verwenden Sie row_number() :

SELECT Album_Name AS Album_Name, a.Album_Date AS Album_Date, a.Page_ID AS PageID,
       p.Image_ID AS Image_ID, p.Image_Small AS Image_Small 
FROM Album_Name a left outer JOIN
     (select p.*, row_number() over (partition by Album_Id order by Image_ID) as seqnum
      from Album_Photos p 
     ) p
    ON a.Album_ID = p.Album_ID and seqnum = 1;