Complicated SQL reference. Forming a query in SQL
I am new to SQL and I need to write a complex query. Could you help me?
I have two tables. One is called PATIENTS and the other is called CASES. Patients have a "patient number" and an entered date. CASES has "patient number", "case number". and "change date". Two tables are associated with a "patient number". There are several βno casesβ. associated with one "patient number", since one patient can have multiple cases.
I need to get the following entries. All patients (from PATIENTS) who have all "sick date" are older than a certain date. So if the date is June 20, 1999. Then I need all patients who have not had cases changed after 06-20-1999
Any help would be appreciated. Thank.
source to share
If you only want patients and ALL cases need to be changed by date, then
Select * From Patients p
Where Exists -- eliminates those with no cases/or no cases before date
(Select * From cases
Where PatientNo = p.PatientNo
And ModifiedDate < [DateValue])
And Not Exists -- to eliminate patients with cases modified after date..
(Select * From cases
Where PatientNo = p.PatientNo
And ModifiedDate >= [DateValue])
If you need case data, use the connection:
Select * From Patients p
Join Cases c on c.PatietNo = p.PatientNo
Where c.Modifed < DateValue
EDIT: change the value still to @Larry's comment below, thanks!
source to share