Tamer Sherif
Tamer Sherif

Reputation: 23

Count records from two tables grouped by one field

I have 2 tables:

  1. doctorreports with fields:
    • DoctorRepID
    • RepName
    • DoctorName
    • DateAdded
  2. hospitals with fields:
    • HospitalID
    • RepName
    • HospitalName
    • DoctorName
    • DateAdded

I need to count the records added by each RepName in doctorreports & hospitals with the DateAdded.

I need it to appear like this:

RepName | DoctorReportsCount | HospitalsReportsCount | DateAdded
        |                    |                       |
  John  |          15        |          12           | 9/4/2012

RepName in doctorsreports table equal RepName in hospitals table.

@bluefeet This is partially what I need, but can we unify the DateAdded field to be if the RepName hasn't added any records in this date then the DateAdded = 0. For example :

RepName | DoctorReportsCount | HospitalsReportsCount | DateAdded
        |                    |                       |
  John  |          15        |          12           | 9/4/2012
  Ann   |          9         |           0           | 9/2/2012
Tamer   |          0         |           12          | 9/1/2012

Upvotes: 2

Views: 544

Answers (2)

Taryn
Taryn

Reputation: 247840

Sounds like you are trying to do this:

select d.RepName, count(d.RepName) DoctorReportsCount, count(h.RepName) HospitalsReportsCount, d.DateAdded from doctorreports d inner join hospitals h on d.RepName = h.RepName group by d.RepName, d.DateAdded

edit:

select * from ( select d.RepName, count(d.RepName) DoctorReportsCount , d.dateadded from doctorreports d group by d.RepName, d.dateadded ) d left join ( select h.RepName, count(h.RepName) HospitalsReportsCount , h.dateadded hDateadded from hospitals h group by h.RepName, h.dateadded )h on d.RepName = h.RepName

see SQL Fiddle with demo

edit #2, if you want to return data for days that are missing, then I would advise creating a table to contain calendar dates, then you can return data for days that are missing. The following should return what you are looking for. Be advised, I created a calendar table for this query:

select COALESCE(d.drep, '') repname,
  COALESCE(d.DCount, 0) DoctorReportsCount,
  COALESCE(h.HCount, 0) HospitalsReportsCount,
  c.dt Dateadded
from calendar c
left join
(
  select repname drep,
    count(repname) DCount,
    dateadded ddate
  from doctorreports
  group by repname, dateadded
) d
  on c.dt = d.ddate
left join
(
  select repname hrep,
    count(repname) HCount,
    dateadded hdate
  from hospitals
  group by repname, dateadded
) h
  on c.dt = h.hdate
  and d.drep = h.hrep

see SQL Fiddle with Demo

If you don't care about the other dates, then this is how you would do it without a date table:

select COALESCE(d.RepName, '') repname,
  COALESCE(d.DoctorReportsCount, 0) DoctorReportsCount,
  COALESCE(h.HospitalsReportsCount, 0) HospitalsReportsCount,
  COALESCE(p.PharmacyReportsCount, 0) PharmacyReportsCount,
  d.dateadded Dateadded
from
(
  select d.RepName,
      count(d.RepName) DoctorReportsCount
    , d.dateadded
  from doctorreports d
  group by d.RepName, d.dateadded
) d
left join
(
  select h.RepName,
    count(h.RepName) HospitalsReportsCount
  , h.dateadded hDateadded
  from hospitals h
  group by h.RepName, h.dateadded
)h
  on d.RepName = h.RepName
  and d.dateadded = h.hDateadded
left join
(
  select p.RepName,
    count(p.RepName) PharmacyReportsCount
  , p.dateadded hDateadded
  from PharmacyReports p
  group by p.RepName, p.dateadded
)p
  on d.RepName = p.RepName
  and d.dateadded = p.hDateadded

see SQL Fiddle with Demo

Upvotes: 1

J. Bruni
J. Bruni

Reputation: 20490

The following SQL statement should bring the results you want:

SELECT 
    RepName,
    SUM(DoctorReports) AS DoctorReportsCount,
    SUM(HospitalReports) AS HospitalReportsCount,
    DateAdded
FROM (
    (SELECT RepName, COUNT(*) AS DoctorReports, 0 AS HospitalReports, DateAdded
     FROM doctorreports
     GROUP BY RepName, DateAdded)
    UNION
    (SELECT RepName, 0 AS DoctorReports, COUNT(*) AS HospitalReports, DateAdded
     FROM hospitals
     GROUP BY RepName, DateAdded)
) AS temp
GROUP BY
    RepName, DateAdded
ORDER BY
    RepName, DateAdded;

Upvotes: 0

Related Questions