score:2

Accepted answer

I would do this way:

For every doctor do a query like this:

/* get all pregrant women for a doctor, group by month and count records */
SELECT MONTH( p.reg_date ), COUNT(*) 
FROM nurse_dets n
INNER JOIN preg_reg_other p 
    ON n.nurse_id = p.nurse_id 
WHERE n.doctor_id = 1301
AND n.reg_date IS NOT NULL
GROUP BY MONTH( p.reg_date )
ORDER BY p.reg_date ASC

This will give you, for every month, the count of pregnant women associated with the doctor with id 1301

Now, you could do a query like this for every doctor and, in every iteration, build a different serie for you chart

In the end you will pass all your series array to your client and fetch the data for highcharts

EDIT

Getting the value of 0 for the months where there are no pregnant women is a litte more complicated, as the group by doesn't return any value if there isn't at least one record to count. Try this way:

SELECT months.num, COUNT( p.preg_id )
FROM 
   ( SELECT 1 AS num
     UNION ALL SELECT 2 
     UNION ALL SELECT 3 
     UNION ALL SELECT 4 
     UNION ALL SELECT 5  
     UNION ALL SELECT 6 
     UNION ALL SELECT 7  
     UNION ALL SELECT 8 
     UNION ALL SELECT 9  
     UNION ALL SELECT 10
     UNION ALL SELECT 11  
     UNION ALL SELECT 12 ) months
LEFT JOIN preg_reg_other p 
    ON months.num = MONTH ( p.reg_date )
INNER JOIN nurse_dets n
    ON n.nurse_id = p.nurse_id
WHERE n.doctor_id = 1301    
GROUP BY MONTH( months.num )
ORDER BY months.num ASC

First we manually build a table with the 12 months and "left join" with the other tables, so if there isn't any record, the final count should result in 0

I'haven't tested the query, but i hope you get the idea


Related Query

More Query from same tag