Table tblReportPatients has 23 PatientIds in it. tblDemographic has a 1 to many relationship and is filled with demographics values for each patient ID. A patient could have multiple "Grade Completed" rows in tblDemographic and each one has an EncounterDate when it was recorded. The query below returns the last "Grade Completed" value for each patient that has one or more "Grade Completed" rows in tblDemographic. Only 20 of those patients has a "Grade Completed", so I get 20 rows. What I would like to do is return 23 rows - one for each row in tblReportPatients - and have the 3 that have no row in tblDemographic show up as either a Null value or "Unreported". The second version of the query tries to use the Iif() function to return "Unreported", but it doesn't work. Any help is appreciated.
Greg
Greg
Code:
SELECT tblReportPatients.PatientID,
[Value] AS Grade
FROM tblReportPatients LEFT JOIN tblDemographic ON tblReportPatients.PatientID = tblDemographic.PatientID
WHERE
(((tblDemographic.ItemName)="Grade Completed")
AND ((tblDemographic.EncounterDate)=(SELECT Max(Q.EncounterDate)
FROM tblDemographic As Q WHERE Q.ItemName='Grade Completed' AND Q.PatientID = tblDemographic.PatientID)));
Code:
SELECT tblReportPatients.PatientID,
IIf(IsNull([Value]),"unreported",[Value]) AS Grade
FROM tblReportPatients LEFT JOIN tblDemographic ON tblReportPatients.PatientID = tblDemographic.PatientID
WHERE
(((tblDemographic.ItemName)="Grade Completed")
AND ((tblDemographic.EncounterDate)=(SELECT Max(Q.EncounterDate)
FROM tblDemographic As Q WHERE Q.ItemName='Grade Completed' AND Q.PatientID = tblDemographic.PatientID)));