Answer the question
In order to leave comments, you need to log in
How to calculate the number of doctors working per week?
Good day, there are two tables in MySQL:
The first is a list of doctors, structure (id, full_name, specialty, status), about 1000 records.
The second one is doctors' weekends (id, doctor_id, start_date, end_date), about 50 records-time intervals per doctor (about 50,000 records in total). start_date, end_date - unix timestamp format.
It is necessary to count the number of doctors who work, i.e. are not on vacation/day off for a given time interval, for example, a week.
If you need to find the number of surgeons for a given interval, for example, inputInterval with borders also in unix timestamp - everything is simple, a type request using a check for non-intersection of intervals in the condition works fine:
SELECT COUNT(doctors.id) AS total_doctors, doctors.speciality
FROM doctors
WHERE NOT EXISTS (
SELECT holidays.id
FROM holidays
WHERE holidays.doctor_id = doctors.id
AND holidays.start_date > inputInterval.end_date OR holidays.end_date < inputInterval.start_date
)
GROUP BY doctors.specialty
HAVING doctors.speciality = "хирург"
Answer the question
In order to leave comments, you need to log in
view query using in condition check for non-intersection of intervals works fine
SELECT COUNT(doctors.id) AS total_doctors, doctors.speciality
JOIN holidays ON holidays.doctor_id = doctors.id AND (holidays.start_date > inputInterval.end_date OR holidays.end_date < inputInterval.start_date)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question