sql - Number of specific associations for model in RoR -
so have model doctor , each doctor has many appointments , each appointment in turn has it's date (as datetime column).
i need show list of, let's 30 doctors, , each doctor, need show number of appoinments or had on 1st of june wonder how that?
the best option can come use nested sql this:
select doctors.*, (select count(*) appointments a.date = 01.06.2014 , a.doctor_id = doctors.id) appointments_number doctors; (i know date not filled correctly lazy google right way, doesnt matter)
so, there easier way it?
group columns of doctors table want select , add count
select d.id, d.name, count(a.id) appointments from doctors d join appointments on a.doctor_id = d.id a.date = '2014-06-01' group d.id, d.name
Comments
Post a Comment