Solved

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

Posted on 2009-05-07
2
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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.

739 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