Solved

SQL Join Problem - Show Records where count (x) is null

Posted on 2009-05-07
2
372 Views
Last Modified: 2012-05-06
I'm stuck on a query and I think the problem has to do with my join syntax.  I have two tables...an HR name list with employee ID as a key and an audit table where employee ID is a foreign key.  I want to write a query that lists all of the employees and their count of audits conducted in the last month, BUT I want to show zero for employees that have created no audits that month.

Currently my query looks like...

SELECT     COUNT(dbo.SafetyAudits.UniqueAuditNo) AS AuditCount,
                      DHHRBinfo.dbo.HRInfo.LastName, DHHRBinfo.dbo.HRInfo.FirstName
FROM         DHHRBinfo.dbo.HRInfo FULL OUTER JOIN
                      dbo.SafetyAudits ON dbo.SafetyAudits.EmployeeID = DHHRBinfo.dbo.HRInfo.EmployeeId
WHERE     (dbo.SafetyAudits.AuditDate > '5/1/09')
GROUP BY DHHRBinfo.dbo.HRInfo.LastName, DHHRBinfo.dbo.HRInfo.FirstName


and if I have 200 employees and 8 did audits in May, my output is only those 8 employees.  I want it to be all 200 employees, with zero for the 192 that didn't do any audits.
0
Comment
Question by:dhadmin
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24326790
You can use a Left outer join
SELECT a.EmployeeID, ISNULL(AuditCount , 0 ) as AuditCount
FROM HRInfo  a
LEFT JOIN  (
     SELECT EmployeeID , COUNT(EmployeeID ) as AuditCount
     FROM dbo.SafetyAudits
     GROUP BY EmployeeID
) B  on a.EmployeeID = b.EmployeeID

0
 

Author Comment

by:dhadmin
ID: 24327006
That general idea worked, thanks.  Some typos and logic errors in the above, so here's a copy of what actually worked.


SELECT     ISNULL(SafetyAudits.AuditCount, 0) AS AuditCount, DHHRBinfo.dbo.HRInfo.FirstName, DHHRBinfo.dbo.HRInfo.LastName
FROM         DHHRBinfo.dbo.HRInfo LEFT OUTER JOIN
                          (SELECT     EmployeeID, COUNT(UniqueAuditNo) AS AuditCount
                            FROM          dbo.SafetyAudits AS SafetyAudits_1
                            GROUP BY EmployeeID) AS SafetyAudits ON DHHRBinfo.dbo.HRInfo.EmployeeId = SafetyAudits.EmployeeID
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now