Solved

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

Posted on 2009-05-07
2
374 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

10 Experts available now in Live!

Get 1:1 Help Now