I've problem in following query. It almost works as per my need but it bring some extra records that are not expected.
SELECT avc.statecode as 'Status', avc.new_dateofvisit as 'Date of Visit',avc.new_accountid,avc.new_titleonlyagency as 'Title Only Agency',account.new_agentid as 'Agency ID'
FROM Filterednew_2015agencyvisitationchecklist as avc
INNER JOIN(
SELECT * FROM FilteredAccount
) as account on avc.new_accountid = account.accountid
WHERE (avc.statecode = 0) and (avc.new_dateofvisit>'1/1/2015')
and
avc.new_dateofvisit not in
(
SELECT max(avc2.new_dateofvisit)
FROM Filterednew_2015agencyvisitationchecklist avc2
WHERE avc2.new_titleonlyagency = 0 AND avc2.new_accountid = avc.new_accountid
)
Expectation from the query are that retrieve records that have Yes in Title Only Agency against their recent date of visit. but it also retrieving other records
Here is what expected from query:
and here is not expected: