Lets say you have a doctors office which has a number of different doctors and many clients. Sometimes a client can be seen by a different doctor each time they come in.Their database consists of the following tables:2 Entity Tables
Clients - ClientID...ClientNameDoctors - DoctorID...DoctorName
OfficeVisits - EventID...ClientID...VisitDate.......DoctorIDPhysicals - EventID...ClientID...PhysicalDate....DoctorIDSurgeries - EventID...ClientID...SurgeryDate.....DoctorID
2/9/2007 12:06:29 PM
you are going to have to create client-doctor combinations then join the visits on from there. your visit joins are going to have to join on both clientid and doctor idyour current select is selecting the latest visit for each patient, then showing only the officevisit doctor.you've got
(patient)-> (latest office)->(doctor) (latest physical) (latest surgery)
(patient,doctor)-> (latest office) (latest physical) (latest surgery)
2/9/2007 1:06:44 PM
Assuming that OfficeVisits, Physicals, and Surgeries are all mutually exclusive (as in, a physical does not count as an OfficeVisit), I'd roll like this:SELECT c.clientID, MAX(v.date), MAX(p.date), MAX(s.date), d.docIDFROM Clients c, OfficeVisits v, Physicals p, Surgeries s, Doctors dWHERE v.docID = d.docID AND p.docID = d.docID AND s.docID = d.docID AND v.clientID = c.clientID AND p.clientID = c.clientID AND s.clientID = c.clientIDGROUP BY d.docID, c.clientID;I don't see that the left outer joins help any in this situation, so just do it straight (I'm also more familiar with the results of WHERE statements, so that's my bias). Also, you have to make sure that the docID is compared for each doctor, otherwise you will get the most recent date for any visit/physical/surgery, regardless of the doctor.
2/10/2007 12:31:19 AM