Displaying records outside of a date range.

G

Guest

I have a query that uses the expression below. As it is, it shows active
patients who have an appointment within the date range the user enters, but
drops the patients that don't. I need it to always show ALL the active
patients, but indicate that they have no appointments in the specified date
range. Any suggestions would be appreciated.


HAVING (((PatientDay.Date) Between [Enter begin date:] And [Enter end
date:]) OR ((Patient.DischargeDt) Is Null)) AND (([All Active Home
Care].Team)<>"HOS")
 
M

Michel Walsh

Hi,




SELECT a.*, EXISTS( SELECT *
FROM appointments AS b
WHERE b.patientID=a.patientID
AND b.appDate BETWEEN date1 AND date2)
FROM clients AS a




or, with a join:

SELECT a.patientID, COUNT(b.patientID)<>0
FROM patients AS a LEFT JOIN
(SELECT patientID
FROM appointments
WHERE dateApp BETWEEN date1 AND date2) AS b
ON a.patientID = b.patientID
GROUP BY a.patientID


With MS SQL Server, you can do the latter more succintly:


SELECT a.patientID, COUNT(b.patientID)<>0
FROM patients AS a LEFT JOIN appointments AS b
ON a.patientID = b.patientID
AND dateApp BETWEEN date1 AND date2
GROUP BY a.patientID




Hoping it may help,
Vanderghast, Access MVP
 
G

Guest

Thank you for the quick response. I think I've gotten past any knowledge
base I thought I might have had, so I'm going to give your logic a shot and
see how it goes.

Thanks!
Bullmann

Michel Walsh said:
Hi,




SELECT a.*, EXISTS( SELECT *
FROM appointments AS b
WHERE b.patientID=a.patientID
AND b.appDate BETWEEN date1 AND date2)
FROM clients AS a




or, with a join:

SELECT a.patientID, COUNT(b.patientID)<>0
FROM patients AS a LEFT JOIN
(SELECT patientID
FROM appointments
WHERE dateApp BETWEEN date1 AND date2) AS b
ON a.patientID = b.patientID
GROUP BY a.patientID


With MS SQL Server, you can do the latter more succintly:


SELECT a.patientID, COUNT(b.patientID)<>0
FROM patients AS a LEFT JOIN appointments AS b
ON a.patientID = b.patientID
AND dateApp BETWEEN date1 AND date2
GROUP BY a.patientID




Hoping it may help,
Vanderghast, Access MVP



Bullmann said:
I have a query that uses the expression below. As it is, it shows active
patients who have an appointment within the date range the user enters,
but
drops the patients that don't. I need it to always show ALL the active
patients, but indicate that they have no appointments in the specified
date
range. Any suggestions would be appreciated.


HAVING (((PatientDay.Date) Between [Enter begin date:] And [Enter end
date:]) OR ((Patient.DischargeDt) Is Null)) AND (([All Active Home
Care].Team)<>"HOS")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top