Search code examples
mysqlcountrecords

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

Solution

  • 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