Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to get the list of doctors with the number of the visits per cabinet of each one between 21-10-2009 and 12-04-2013. The list must contain all the doctors even if some of them have no visits.
This is what I managed to do:

SELECT doctors.IdDoctors, doctors.Name, doctors.Surname,
COUNT(visits.IdPacients) AS Visits, cabinets.Name, visits.DateHour
FROM doctors
JOIN visits USING (IdDoctors)
JOIN cabinets USING (IdCabinets)
WHERE IdDoctors = 1 AND DATE(visits.DateHour) > 21-10-2009
AND DATE(visits.DateHour) < 12-04-2013 GROUP BY cabinets.Name;

but I need something like this:

IdDoctors     Visits        Name
IdDoctors 1 | ---- |cabinet.Name 3|
IdDoctors 1 | ---- |cabinet.Name 7|
IdDoctors 2 | ---- |cabinet.Name 5|
IdDoctors 2 | ---- |cabinet.Name 7|
IdDoctors 2 | ---- |cabinet.Name 1|
IdDoctors 3 | null |null |
IdDoctors 3 | null |null |
etc....
IdDoctors 98| ---- |cabinet.Name 1|
IdDoctors 98| ---- |cabinet.Name 3|
Posted

1 solution

First you do not state which SQL you are using so I have assumed SQL Server. When grouping you need to include all fields that do not have a mathematical calculation in and you only need the field required back from the query in the select part of the query.

You can try something like
SQL
SELECT doctors.Name, doctors.Surname,COUNT(visits.IdPacients) AS Visits, cabinets.Name
 FROM doctors right join visits on doctors.iddoctors = visits.IdDoctors 
right join cabinets on visits.IdCabinets = cabinets.idcabinet
WHERE visits.DateHour  > '21-Oct-2009'
 AND visits.DateHour < '12-Apr-2013' GROUP BY cabinets.Name, doctors.Name, doctors.Surname


BUT; You may have to modify it to suit the table structures you have (unknown to us)

Also have used 21-Oct-2009 to get round different country codes, but this may be different in your situation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900