2016-05-11 3 views
1

は、私のような結果が見てする必要があります。より多くの予約があり予約表は、この患者ごとに予約されている医師の数を数えるには?

GPS Table 

PatientID DoctorID DATE 
Patient1 Doctor1 2016-02-16 
Patient1 Doctor1 2016-04-08 
Patient1 Doctor2 2016-06-09 
Patient2 Doctor3 2017-01-02 
Patient2 Doctor6 2016-12-01 
Patient3 Doctor1 2016-07-12 

のように見えます

PatientID Doctors 
Patient1 3 
Patient2 2 
Patient3 1 

が、私は一例として、この表を与えています。 また、その人が医師のために2回予約されている場合は、同じ医師を数えないようにする必要があります。

私が今持っているコードは次のとおりです。

select Bookings.PatientID, count(Bookings.DoctorID) as Doctors from Bookings where Bookings.DoctorID; 

は、任意の助けをありがとう!

答えて

2

利用グループの異なる患者と医師

select Patient, count(*) 
from (
select distinct Bookings.PatientID as Patient ,DoctorID as Doctors 
from Bookings ) as t 

Group by Patient; 
+0

ニース.. +1 .. btw私はいくつかの自由な時間があれば私の質問を更新しました[それ]を見てください(http://stackoverflow.com/questions/37160002/how-do-i -check-a-updates-before-updating) – Shafizadeh

+0

@Shafizadehあなたの新しい質問を説明してください。cange dがどこにあるのかわかりません...(ただし、あなたはOPではありませんか?..) – scaisEdge

0

と一時テーブルの上にあなたがcount(distinct)を探していることによって:

select b.PatientID, count(distinct b.DoctorID) as NumDoctors 
from Bookings b 
group by b.PatientID; 
+0

同じ列ですか? – user3802077

0

はこれを試してみてください。

SELECT PatientID, count(DoctorID) FROM `GPS ` group by PatientID 
0

私はこれをしなかったし、それは働いた:

select PatientID as Patient, count(distinct(DoctorID)) as Doctors from Bookings, Patients group by PatientID; 

ありがとうございました!

関連する問題