SQL Server - Join two tables based on most recent date

I have two tables:

       A
---------------
No.    Name
1      J.Smith
2      R.Alan
3.     D.Williams


       B
----------------------------
ID.    Date        Address    No.(FK from table A) 
1      10/03/01   blah blah   1
2      08/02/05   blah blah   2
3.     12/01/02   blah blah   3
4.     03/07/11   blah blah   1
5.     30/03/09   blah blah   2

I want to join the two tables together ommiting rows with duplicate names based on the most recent date from table B. For example, if I join table A and B together using a left inner join, I will get two rows for J. Smith (based on rows 1 and 4 from table B). I would like to exclude the one with the older date (row 1 from table B).

How can I do this?

EDIT:

I need to include all columns from table B.

Answers


Maybe something like this:

;WITH CTE
AS
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY tblB.[No] ORDER BY tblB.[Date]) AS RowNbr,
        tblB.*
    FROM
        B AS tblB
)
SELECT
    *
FROM
    A AS tblA
    LEFT JOIN CTE
        ON tblA.No=CTE.No
        AND CTE.RowNbr=1

WITH [max_dates] AS (
    SELECT [No], MAX([Date]) AS [Date]
    FROM [TableB]
    GROUP BY [No]
)
SELECT [a].[Name], [b].[Date]
FROM [TableA] AS [a]
JOIN [max_dates] AS [b] ON ([a].[No] = [b].[No])
ORDER BY [a].[No] ASC

Need Your Help

What are your favorite open source or free library for .NET Charts and Gauges?

.net charts open-source

For a in-house hardware monitoring utility, I need to use gauges and charts to help visualize various metrics. Is there any good open source or free charting and gauge library available for .NET? I...