Henry Taylor
Henry Taylor

Reputation: 91

Merging Tables Using MS-Access

I've created a database with two tables: Table1 and Table2.

Table1 Contains the following data:

Account#    Account Owner
1           Person1
2           Person2

Table2 Contains:

Account#      Date Modified     Status
1             5/15/2011         Open
2             4/15/2011         Closed
1             5/21/2011         Pending
2             6/15/2011         Cancelled

Using SQL, how can I merge these two tables so that It will give me the latest status for each account?

Upvotes: 1

Views: 155

Answers (1)

Renaud Bompuis
Renaud Bompuis

Reputation: 16776

This will do it:

SELECT Table1.[Account#],
       Table1.[Account Owner],
       L.LastOfDate,
       Table2.Status
FROM   ((SELECT Table2.[Account#],
                Max(Table2.[Date Modified]) AS LastOfDate
        FROM   Table2
        GROUP  BY Table2.[Account#]) AS L
        INNER JOIN Table1
          ON L.[Account#]=Table1.[Account#])
       INNER JOIN Table2
         ON (L.[Account#]=Table2.[Account#])
            AND (L.LastOfDate=Table2.[Date Modified]); 

Will result in:

Account#    Account Owner    LastOfDate     Status
1           Person1          05/21/2011     Pending
2           Person2          06/15/2011     Cancelled

Upvotes: 1

Related Questions