Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-07
2
Medium Priority
?
380 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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
Integration Management Part 2
Loops Section Overview

916 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