0

除算の例をたくさん見てきましたが、次の問題を解決する方法を理解するのに役立つものはありませんでした:

関係は次のとおりです。

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

問題は、患者がインフルエンザにかからず、その都市のすべての診療所で治療を受けた患者 ID と都市のすべてのペアを見つけることです。

私が見てきたことから、インフルエンザのない患者を ....? で割る必要があります。特定の都市のすべての診療所を含むテーブルであるべきだと思いますが、どのようにしてそれを取得し、可能な都市ごとに取得できますか?

ありがとう

4

1 に答える 1

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 

これにより、すべてのテーブルが結合され、 と をリンクできるようにPIDなりCIDます。次に、インフルエンザの診断を含まない患者に関する情報のみが必要です。その後、患者情報と診療所の都市でグループ化し、都市ごとに患者が通った診療所の数をカウントできるようにします。患者 A は最初の都市のクリニック 1 に行き、インフルエンザの治療を受けることができます。また、患者 A が別の都市の別のクリニックでインフルエンザの治療を受けたとしても、あなたが質問の言い回しでその人を示すことができます。最初の都市/診療所の結果がアップ....それは意味がありますか?

次に、これら 2 つの結果を結合する必要があります。各都市の診療所の数が、インフルエンザと診断されずに患者が都市ごとに訪れた診療所の数に等しい場合、次のようになります。

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 には診療所が 1 つしかなく、そこではインフルエンザと診断されていないため、結果にその人が表示されることを期待する必要があります...

私が変更したこと:

  • Patient_Doctor: PID、DID、疾患、重症度
  • Patient: PID、PName
  • Clinicの代わりにによってDoctor結合されるようになりましたCIDCName
  • Disease同じ情報を何度も繰り返さないように、別のテーブルを作成することを強くお勧めします。このテーブルには、フィールドDisease_IDとが含まれますDisease_Description。その後、疾患は によって患者に関連付けられDisease_IDます。
于 2013-10-31T18:37:08.067 に答える