I am trying to find the Max date (latest date) for each accountid in my table FilteredVisit. In my table I have the fields: accountid, casereference and visitdate.
I want the latest date for each accountid. Some have multiple visits some only one visit.
I have 2,464 records in the visit table and I know that I have 1,185 unique accounts in that table.
So I'm expecting 1,185 records the result of the query. But having tried the two methods below I get different answers neither of which match the target of 1,185 records.
These are the methods I've tried:
Select accountid, casereference, visitdate
from (Select accountid, casereference, visitdate,
ROW_NUMBER() over(partition by visitdate order by accountid) as rn from FilteredVisit) as T
where rn = 1
order by hiw_accountid
--which returns 1,215 records
SELECT accountid, casereference, visitdate
FROM FilteredVisit S
WHERE visitdate=(SELECT MAX(visitdate) FROM Filteredhiw_inspection WHERE hiw_accountid = S.hiw_accountid)
ORDER BY accountid
-- which returns 1,165 records
which solution do I trust? What have I done wrong? Thanks
It’s a backend db for a CRM. Standard setup as far as I know. Thanks for the feedback which has helped update this post.