Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
What I want to do is join Persons and Orders but get only the latest order of the person. This is the result I want:
PersonId Name OrderId OrderDate-------- ---- ------- ---------1 John 2 02.012 James 4 03.01
How can I do this?
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2011-04-11 : 06:48:48
SELECT p.PersonID, p.Name, f.OrderID, f.OrderDateFROM (SELECT OrderID, PersonID, OrderDate, ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY OrderDate DESC) AS YakID FROM dbo.Orders) AS fINNER JOIN dbo.Persons AS p ON p.PersonID = f.PersonIDWHERE f.YakID = 1N 56°04'39.26"E 12°55'05.63"