Question:
I need to join three (3) different tables but I just need to select the latest record in the third table. How to do it in Microsoft SQL Server (MSSQL)?
Table1: PersonId (Primary Key)CodeNameTable2: AddressId (Primary Key)PersonId (Foreign Key - Person(Id))TypeAddressDetailTable3: ImageId (Primary Key)PersonId (Foreign Key - Person(Id))ImagePathUploadDate
*Note: This is for tutorial purpose only.
Answer:
Consider that we have the above tables.
SELECT * FROM Person INNER JOIN Address ON Address.PersonId=Person.Id OUTER APPLY (SELECT TOP 1 * FROM Image WHERE Image.PersonId=Person.Id ORDER BY UploadDate DESC) Image