从一个领域划分两个表盘点记录(Count records from two tables group

2019-09-22 10:39发布

我有2个表:

  1. 同场doctorreports:
    • DoctorRepID
    • RepName
    • DoctorName
    • 添加日期
  2. 医院与字段:
    • HospitalID
    • RepName
    • HospitalName
    • DoctorName
    • 添加日期

我需要计算在doctorreports和医院,的设置DateAdded每个RepName添加的记录。

我需要它看起来是这样的:

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

在doctorsreports表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

Answer 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

看到SQL拨弄演示

编辑#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拨弄演示

如果你不关心其他日期,那么这就是你会怎么做没有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拨弄演示



Answer 2:

下面的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;


文章来源: Count records from two tables grouped by one field