I am attempting to retrieve the most recent address associated with a name (as there are usually multiple addresses per name). I have created the following query with a subquery in my join. I am able to pull back one address per party as I wanted but the address/city doesn't always match up or the address that's returning is not the most recent address (per the date associated). Am I writing the subquery with MAX wrong? Is there a way to make sure the correct city goes with the correct address?
select
[demographics].[DOB],
Address1,
Address2,
City,
State,
Zip
from [demographics]
left join (select
[address].[demographic_ID],
max([address].[address1]) as Address1,
max([address].[address2]) as Address2,
max([address].[city]) as City,
max([states].[state_title]) as 'State',
max([dbo].[address].[zip]) as Zip
from [address]
inner join [states] on [address].[state_ID] = [states].[state_ID]
group by [address].[demographic_ID]) as AddressSub
on [demographics].[demographic_ID] = AddressSub.[demographic_ID];