SELECT t.ClientId, t.MaxDate, o.OrderNumber FROM (SELECT ClientId, MAX(Date) as MaxDate FROM dbo.tblOrders GROUP BY ClientId) t INNER JOIN dbo.tblOrders o ON t.ClientId = o.ClientId AND t.MaxDate = o.Date
If you are using an RDBMS that supports windowing functions, such as SQL Server 2005+, this can also be done as follows:
SELECT t.ClientId, t.OrderNumber, t.Date FROM (SELECT ClientId, OrderNumber, Date, ROW_NUMBER() OVER(PARTITION BY ClientId ORDER BY Date DESC) as RowNum FROM dbo.tblOrders ) t WHERE t.RowNum = 1
source share