计算按一个字段分组的两个表的记录

时间:2012-09-14 17:35:36

标签: mysql count records

我有两张桌子:

  1. 医生报告字段:
    • DoctorRepID
    • RepName
    • DoctorName
    • DateAdded
  2. 有田地的医院:
    • HospitalID
    • RepName
    • HospitalName
    • DoctorName
    • DateAdded
  3. 我需要计算每个RepName在doctorreports&具有DateAdded的医院。

    我需要它看起来像这样:

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

    医生报告表中的RepName等于医院表中的RepName。

    @bluefeet这部分是我需要的,但我们可以统一DateAdded字段,如果RepName在此日期没有添加任何记录,那么DateAdded = 0.例如:

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

2 个答案:

答案 0 :(得分:1)

听起来你正试图这样做:

<击> 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

编辑:

<击> 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

编辑#2,如果你想要丢失缺失天数的数据,那么我建议创建一个包含日历日期的表,然后你可以返回缺少的数据。以下内容应该返回您要查找的内容。请注意,我为此查询创建了一个日历表:

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

请参阅SQL Fiddle with Demo

如果你不关心其他日期,那么如果没有date表,你就会这样做:

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

请参阅SQL Fiddle with Demo

答案 1 :(得分:0)

以下SQL语句应该带来您想要的结果:

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;