I am trying to find the last Referring MD's name for a patient and cannot quite get it. When I leave in the ORDER BY, it looks at the last Visit I entered and because that was NULL, it tells me NULL. I had a visit prior to that that contained the MD's name and thats the one I want to see.
DOS 12/29/2009 - No Referring MD choosen on Patient Visit.
DOS 12/28/2009 - Dr Jones choosen as the Ref MD
How can I code this to pull the last none NULL MD from my table. If the end result is no Ref MD choosen on any Visit, go ahead and give me the NULL.
SELECT TOP 1
'250.LastVisitReferringMD' = ref.ListName
LEFT JOIN DoctorFacility ref ON pv.ReferringDoctorId = ref.DoctorFacilityId
INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId
pp.PatientProfileId = '48'
-- ORDER BY
-- pv.Visit desc