How to count how many doctors are booked by each patient?

后端 未结 4 598
礼貌的吻别
礼貌的吻别 2021-01-24 00:12

I need the results to look like:

PatientID   Doctors
Patient1    3
Patient2    2
Patient3    1

The booked table looks like this



        
相关标签:
4条回答
  • 2021-01-24 00:53

    try this :

    SELECT PatientID, count(DoctorID) FROM `GPS ` group by PatientID
    
    0 讨论(0)
  • 2021-01-24 01:07

    Use Group by over a temp table with distinct patient and doctor

     select Patient, count(*)
     from (
     select distinct Bookings.PatientID as Patient ,DoctorID as Doctors 
     from Bookings  ) as t
    
     Group by Patient;
    
    0 讨论(0)
  • 2021-01-24 01:08

    You are looking for count(distinct):

    select b.PatientID, count(distinct b.DoctorID) as NumDoctors
    from Bookings b
    group by b.PatientID;
    
    0 讨论(0)
  • 2021-01-24 01:09

    I did this and it worked:

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

    Thank you for the help!

    0 讨论(0)
提交回复
热议问题