Вопрос

I have seen many examples of division but none helped me understand how to solve the following problem:

Relations are:

Patient (Pid,   Did,   Pname, Disease, Severity)
Doctor  (Did,   Cname, Dname, Specialty)
Clinic  (Cname, Mname, Type,  City)

Question is: Find all pairs of patient ID and city such that the patient doesn't suffer from flu, and was treated in all the clinics in that city.

From what I've seen I need to divide the patients without flu by....? I think that it should be a table with all the clinics in a certain city, but how do I get that, and for each possible city?

Thanks

Это было полезно?

Решение

so... this is kind of complicated... pay special attention to the sample data I included in the tables.

http://sqlfiddle.com/#!2/04eac4/15/0
http://sqlfiddle.com/#!2/04eac4/17/0

I have no idea where division was supposed to take place...?

First we need to know how many clinics are in each city:

select city, count(cid) as c_count from clinic group by city

Then we need to know where each patient has been treated for anything besides the flu...

  select  patient.pid, patient.pname, clinic.city,
          count(distinct clinic.cid) as p_c_count 
  from    patient  
          join patient_doctor 
            on patient.pid = patient_doctor.pid  
          join doctor 
            on patient_doctor.did = doctor.did 
          join clinic 
            on doctor.cid = clinic.cid 
  where   patient_doctor.disease not in ('Flu') 
  group   by patient.pid, patient.pname, clinic.city 

This joins all the tables together so that we can link PID and CID. Then we want only information that on patients that does not include a diagnosis of the flu. After, we group by the patient information and the clinic city so that we can count the number of clinics that patient went to for each city. Keep in mind, Patient A can go to Clinic 1 in the first city and be treated for the flu and even if he gets treated for the flu at a different clinic in a different city, the way you phrased the question allows that person to show up in results for the first city/clinic.... Does that makes any sense?

Then we need to join those two results. If the number of clinics in each city is equal to the number of clinics a patient visited per city, without being diagnosed with the flu, we get this:

select  p_info.pid, c_ct_info.city

from   (select city, count(cid) as c_count from clinic group by city) as c_ct_info 

       join  (
            select  patient.pid, patient.pname, clinic.city,
                    count(distinct clinic.cid) as p_c_count 
            from    patient  
                    join patient_doctor 
                      on patient.pid = patient_doctor.pid  
                    join doctor 
                      on patient_doctor.did = doctor.did 
                    join clinic 
                      on doctor.cid = clinic.cid 
            where   patient_doctor.disease not in ('Flu') 
            group   by patient.pid, patient.pname, clinic.city ) as p_info
         on c_ct_info.city = p_info.city 

where  c_count = p_c_count;

Now. This excludes anyone who just visited one clinic and wasn't diagnosed with the flu but didn't go any of the other clinics in that city.

Just in case that sqlfiddle dies one day... here is the tables and data I used:

Create table Clinic (
   CID     int not null primary key,    

   Cname   varchar(100), 
   Mname   varchar(100),  
   Type    tinyint, 
   City    varchar(100) 
);

insert into Clinic values (1,'Clinic 1','Mname 1', 1, 'City 1'); 
insert into Clinic values (2,'Clinic 2','Mname 2', 1, 'City 1'); 

insert into Clinic values (3,'Clinic 3','Mname 3', 1, 'City 2');  


Create table Doctor (
   DID       int not null primary key,    

   Dname     varchar(100), 
   Specialty varchar(100), 

   CID int, 
   foreign key (CID) references Clinic (CID)
);

insert into Doctor values (1, 'House',   'Internal Medicine', 1); 
insert into Doctor values (2, 'Dr Who',  'General Practice',  1); 
insert into Doctor values (3, 'Dr Dave', 'General Practice',  1); 

insert into Doctor values (4, 'Dr Four', 'General Practice',  2);  

insert into Doctor values (5, 'Dr Five', 'General Practice',  3);  
insert into Doctor values (6, 'Dr Six',  'General Practice',  3); 


create Table Patient (
   PID     int not null primary key,
   PName   varchar(100)
);

insert into Patient values (1, 'P. One'); 
insert into Patient values (2, 'P. Two'); 
insert into Patient values (3, 'P. Three'); 
insert into Patient values (4, 'P. Four'); 
insert into Patient values (5, 'P. Five');  



Create table Patient_Doctor (
   PDID      int not null auto_increment primary key, 

   PID       int not null, 
   DID       int, 

   Disease   varchar(100), 
   Severity  tinyint,

   foreign key (PID) references Patient (PID),
   foreign key (DID) references Doctor  (DID)
);


insert into Patient_Doctor values (null, 1, 1, 'Flu',   1);
insert into Patient_Doctor values (null, 1, 4, 'Flu',   1);
insert into Patient_Doctor values (null, 1, 5, 'Flu',   1);
-- shouldn't be in our results because they were diagnosed with the flu in each city 

insert into Patient_Doctor values (null, 2, 2, 'Other', 1);
insert into Patient_Doctor values (null, 2, 4, 'Other', 1);
insert into Patient_Doctor values (null, 2, 5, 'Other', 1);
-- should be in our results because they attended every clinic in every city and
-- did not get diagnosed with the flu at any location

insert into Patient_Doctor values (null, 3, 1, 'Other', 1);
insert into Patient_Doctor values (null, 3, 4, 'Other', 1);
insert into Patient_Doctor values (null, 3, 6, 'Flu',   1);
-- should show up in our results for City 1 because they attended all the clinics in that 
-- city and were not diagnosed with the flu. this person should NOT show up in our results
-- for city 2 because they were diagnosed with the flu at a clinic there.  

insert into Patient_Doctor values (null, 4, 3, 'Other', 1); 
-- should NOT show up in any results.  although they weren't diagnosed with the flu, they
-- did not attend each clinic in that city. 

insert into Patient_Doctor values (null, 5, 2, 'Flu',   1); 
--  should NOT show up in results... meets none of the criteria 

if we would add this data:

insert into Patient values (6, 'P. Six');  
insert into Patient_Doctor values (null, 6, 5, 'Other', 1); 

we should expect to see that person in our results because there's only one clinic in city 2 and they were not diagnosed with the flu there...

Things I changed:

  • Patient_Doctor: PID, DID, Disease, Severity
  • Patient: PID, PName
  • Clinic and Doctor are now joined by CID instead of CName
  • I highly suggest you create another table for Disease so that you are not repeating the same information over and over again. This table would include the fields Disease_ID and Disease_Description. The disease would then be linked to the patient by the Disease_ID.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top