关系代数一对多,除法

时间:2013-10-31 02:20:51

标签: sql relational-database relational-algebra

我见过很多分裂的例子,但没有人帮助我理解如何解决以下问题:

关系是:

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

问题是:查找所有患者身份证和城市对,以便患者不患流感,并在该城市的所有诊所接受治疗。

从我看到的情况来看,我需要将没有感冒的患者分开......我认为它应该是某个城市所有诊所的表格,但我如何得到它,以及每个可能的城市呢?

谢谢

1 个答案:

答案 0 :(得分:1)

所以......这有点复杂......要特别注意表格中包含的样本数据。

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

我不知道应该在哪个部门进行......?

首先,我们需要知道每个城市有多少诊所:

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

然后我们需要知道除了流感之外,每个患者在哪里接受治疗......

  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 

这会将所有表连接在一起,以便我们可以链接PIDCID。那么我们只想要那些不包括流感诊断的患者的信息。之后,我们根据患者信息和诊所城市分组,以便我们可以计算患者去每个城市的诊所数量。请记住,患者A可以去第一个城市的诊所1并接受流感治疗,即使他在不同城市的另一家诊所接受流感治疗,您提出问题的方式也允许该人显示第一个城市/诊所的结果......这有什么意义吗?

然后我们需要加入这两个结果。如果每个城市的诊所数量等于每个城市患者就诊的诊所数量,而没有被诊断出患有流感,我们可以得到:

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;

现在。这不包括刚去过一家诊所并且没有被诊断患有流感但没有去过那个城市的任何其他诊所的人。

万一sqlfiddle有一天死了......这是我用过的表和数据:

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 

如果我们要添加这些数据:

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

我们应该期望在我们的结果中看到那个人,因为在城市2中只有一家诊所,他们没有被诊断出患有流感...

我改变的事情:

  • Patient_Doctor:PID,DID,疾病,严重程度
  • Patient:PID,PName
  • ClinicDoctor现已加入CID而不是CName
  • 我强烈建议您为Disease创建另一个表格,这样您就不会一遍又一遍地重复相同的信息。此表格包含字段Disease_IDDisease_Description。然后通过Disease_ID将疾病与患者联系起来。