Sepertinya Anda mencoba melakukan ini:
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
lihat SQL Fiddle dengan demo
edit #2, jika Anda ingin mengembalikan data untuk hari-hari yang hilang, maka saya sarankan membuat tabel yang berisi tanggal kalender, maka Anda dapat mengembalikan data untuk hari-hari yang hilang. Berikut ini harus mengembalikan apa yang Anda cari. Maklum, saya membuat tabel kalender untuk kueri ini:
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
lihat SQL Fiddle dengan Demo
Jika Anda tidak peduli dengan tanggal lainnya, maka beginilah cara Anda melakukannya tanpa date
tabel:
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