我需要结果看起来像:
PatientID Doctors
Patient1 3
Patient2 2
Patient3 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
有更多的预订,但我只是以这张桌子为例。此外,我需要确保,如果该人为该医生预约了两次,则不会计入同一位医生。
我现在的代码是:
select Bookings.PatientID, count(Bookings.DoctorID) as Doctors from Bookings where Bookings.DoctorID;
谢谢你的帮助!
在具有不同患者和医生的临时表上使用分组
select Patient, count(*)
from (
select distinct Bookings.PatientID as Patient ,DoctorID as Doctors
from Bookings ) as t
Group by Patient;
您正在寻找count(distinct)
:
select b.PatientID, count(distinct b.DoctorID) as NumDoctors
from Bookings b
group by b.PatientID;
试试这个:
SELECT PatientID, count(DoctorID) FROM `GPS ` group by PatientID
我做了这个,它起了作用:
select PatientID as Patient, count(distinct(DoctorID)) as Doctors from Bookings, Patients group by PatientID;
谢谢你的帮助!