We have the information about patients in a database with the following schema P
ID: 3844379 • Letter: W
Question
We have the information about patients in a database with the following schema Prescription (PatientID, Drug, Start, End, Physician, Diagnosis, and NewSymptom PatientID, Diagnosis, Start, End) Some Clarifications 1. In Prescription, Start is the date the patient started taking the drug, andEnd is the last date the patient took the drug. To simplify the query, assume that no dates are missing. Physician is the physician who prescribed the drug, and Diagnosis is the condition for which the physician prescribed it. (Assume that, although a drug may be prescribed for multiple conditions, when a physician prescribes a drug for a patient (s)he prescribes it because of a single condition.) 2. NewSymptom stores problems that patients have developed that apparently might be side effects of medications. Diagnosis is the physician's description of the apparent side effect. Start and End are the starting and ending dates of the symptom. Again, assume End is never blank. 3. Assume dates are stored as integers, so that larger integers correspond to later dates. (One commonway is what's called a Julian Date [nothing to do with Julius Caesar where we might store today's date as 2012094 today is the 94th day of 2012. But all you need care about is that the dates are in the right order.)Explanation / Answer
Hi,
1. Here we have to check if the symptom has occured during which time and then fetch the physician from prescription according to these dates, hence we can use a simple join over patientId and match dates like the below query.
select Physician from Prescription p,sNewSymptom s where s.diagnosis='infrancaninophilia' and s.start>=p.start and s.end<=p.end and s.patientID=p.patientID;
the above query breaks down as, get all patients who have side affect as infrancaninophilia, for those patient ids, check if the symptom start and end dates fall into the time period of any prescription.
3. For this one, we will need self join on prescription table such that the patient ids and dates are same.
select a.Physician,b.Physician,a.patientId from Prescription a,Prescription b where a.PatientId=b.PatientId and a.start=b.start and b.end=b.end and a.Physician!=b.Physician;
4.To get all patients with certain symptoms we need to use group by on symptom and can use aggregate function to get the one with first start date.
select PatientId,min(start) from NewSymptom group by diagnosis order by min(start);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.