I have two tables
Sample1D:
Customer_ID |
Account_Name |
Address |
Leave_Date |
1 |
Bob's Bikes |
123 Main St |
6/7/2013 |
2 |
Bob's Bikes Warehouse |
123 Main St |
6/7/2013 |
45 |
Sally's Salon |
457 118th St |
1/3/2014 |
19 |
Pedro's Pizza |
843 Query Rd |
9/2/2010 |
Sample2S:
Contact_Name |
Account_Name |
Address |
Enter_Date |
Jane Johnson |
Jane's Bowling |
123 Main St |
10/9/2013 |
Tammy Barker |
Bob's Boats Outlet |
2900 8th St |
7/29/2013 |
Bob Barker |
Bob's Banana Boats |
2900 8th St |
7/29/2013 |
Sam Smith |
Sam's Salads |
457 118th St |
8/3/2015 |
Dan Jackson |
Dan's Donuts |
843 Query Rd |
9/2/2010 |
What i am trying to do is run a query to join the tables based on Address. When the addresses are the same, provide the relevant columns. I believe i altered someones code on this forum to select the most recent date row based on address and match it with the other table. The issue i am running into being new to Access SQL Queries is creating one that takes only one row. In some instances there will be two rows with the same recent Leave Date for one address. For my purposes, i just want one. So for example, the first two rows in Sample1D, have the same address and leave date but i only need one two match up with the Sample2S table. Likewise with the Sample2S table, If it has the same address twice with the same Enter Date, i only want one of them to match in the JOIN. So basically, i want ZERO duplicate addresses in the join. If it has different dates, select the most recent. If not, either is fine. I have searched far and wide and haven't found code that tailors to this.
I have tried using
Code:
SELECT t1.*, Latest.*
FROM Sample2S t1
INNER JOIN
(
SELECT C.*
FROM SampleD1 C
INNER JOIN
(SELECT Address, MAX(Leave_Date) as MyDate
FROM Sample1D
Group By Address
) t2
ON C.Address= t2.Address
AND C.Enter_Date = t2.MyDate
) Latest
On Latest.Address = t1.Address
This however still provided duplicate Addresses in the result. (I believe due to having the same date)
I'm not sure If i'm close or miles away. Help is appreciated!