I have a table diagnoses
with 3 fields
id(PK), created_date(DateTime), doctor_id(Foreign Key).
doctor1 = 4 (doctor_id),
and
doctor2= 11(doctor_id).
doctor1 arrives in morning and doctor 2 in evening. As we all know when we run Count(*) function the date that comes is the date of first found record. So I want to print something like this
Date | Am-TimeIn | Doctor1 (Count) | Pm-TimeIn | Doctor2 (Count)
so that if doctor1
was absent and doctor2
was present, row gets printed, similarly for doctor2'S ABSENCY.
It requires full outer self join
, group by clause on Date(created_date) so that all the records sum up on daily basis for each doctor. Currently I have this, but its not working
SELECT Date(CASE WHEN (a.created_date IS NOT NULL) THEN a.created_date ELSE b.created_date END) 'Date', a.`doctor1`, TIME(a.created_date) 'AM-TimeIn', b.`doctor2`, TIME(b.created_date) 'PM-TimeIn'
From (select created_date, count(*) doctor1 FROM diagnoses WHERE doctor_id = 4 GROUP BY DATE(created_date)) a
full join
(select created_date, count(*) doctor2 FROM diagnoses WHERE doctor_id = 11 GROUP BY DATE(created_date)) b
ON DATE(b.created_date) = DATE(a.created_date);