Solved

SQL JOIN - Show all Employees and Specific Records from joining table

Posted on 2011-03-25
6
195 Views
Last Modified: 2012-05-11
The scope on my project has changed a bit.  The query below worked fine until management decided that they wanted to show employee's that had zero time entered as well.

I now need to show a list of employee's hours (even if there is no record of hours entered), along with their Office, Skill Level and Department for a specifc date range.  Before I didn't have to show the employee if no time was entered.

 
Needed Output:

 EmployeeName        EBDate               EBChgHours EBNonHours OffName DeptName SKDesc
 ------------        ------               ---------- ---------- ------- -------- ------
 Doe, John E (1111)  2010-02-01 00:00:00  10.75      2.25       City    DeptA    Manager
 Abe, Jane A (2222)                       0          0          CityB   DeptB    Associate

Open in new window


Original Query:
 
SELECT     Employee.Emplname + ', ' + Employee.Empfname + ' (' + Employee.Empnum + ')' AS EmployeeName, EmpBudHRS.EBDate, EmpBudHRS.EBChgHours, 
                      EmpBudHRS.EBNonHours, Office.OffName, Department.DeptName, SkillLevel.SkDesc
FROM         Employee LEFT OUTER JOIN
                      EmpBudHRS ON Employee.ID = EmpBudHRS.EBEmpID RIGHT OUTER JOIN
                      Office ON Employee.Empoff = Office.OffID INNER JOIN
                      Department ON Employee.Empdept = Department.DeptID INNER JOIN
                      SkillLevel ON Employee.Emplevel = SkillLevel.SkID
WHERE     (EmpBudHRS.EBDate BETWEEN @begdate AND @enddate)
ORDER BY Employee.Emplname

Open in new window


Thanks
0
Comment
Question by:ITMcmcpa
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35214875
How this query works ?
SELECT  Employee.Emplname + ', ' + Employee.Empfname + ' (' + Employee.Empnum
        + ')' AS EmployeeName ,
        EmpBudHRS.EBDate ,
        ISNULL(EmpBudHRS.EBChgHours, 0) ,
        ISNULL(EmpBudHRS.EBNonHours, 0) ,
        Office.OffName ,
        Department.DeptName ,
        SkillLevel.SkDesc
FROM    Employee
        INNER JOIN Office ON Employee.Empoff = Office.OffID
        INNER JOIN Department ON Employee.Empdept = Department.DeptID
        INNER JOIN SkillLevel ON Employee.Emplevel = SkillLevel.SkID
        LEFT OUTER JOIN EmpBudHRS ON Employee.ID = EmpBudHRS.EBEmpID
WHERE   ( EmpBudHRS.EBDate BETWEEN @begdate AND @enddate )
ORDER BY Employee.Emplname

Open in new window

0
 

Author Comment

by:ITMcmcpa
ID: 35214921
Unfortunately this query still only pulls Employees that have a record (time entered) in the EmpBudHRS table.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35214946
Just to test - Try this query. Getting records those have no record in Hours table ?
SELECT * 
FROM Employee E
	INNER JOIN EmpBudHRS H ON E.ID = H.EBEmpID
WHERE H.EBDate BETWEEN @begdate AND @enddate

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 35215063
Hi ITMcmcpa,

If I have understood this correctly you are currently showing staff who had hours logged between two dates (@begdate AND @enddate)

and now you need to show all staff who fall into this bracket along with any staff who have no entry at all in the EmpBudHRS table.

If this is right then your current WHERE clause will exclude anyone with not entry in the EmpBudHRS table.

so try changing

WHERE     (EmpBudHRS.EBDate BETWEEN @begdate AND @enddate)

to

WHERE     (EmpBudHRS.EBDate BETWEEN @begdate AND @enddate) OR EmpBudHRS.EBDate IS NULL

and I believe you will get your missing details.

Hope this helps,

Mark
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 35215069
Try this one
SELECT  Employee.Emplname + ', ' + Employee.Empfname + ' (' + Employee.Empnum
        + ')' AS EmployeeName ,
        EmpBudHRS.EBDate ,
        ISNULL(EmpBudHRS.EBChgHours, 0) ,
        ISNULL(EmpBudHRS.EBNonHours, 0) ,
        Office.OffName ,
        Department.DeptName ,
        SkillLevel.SkDesc
FROM    Employee
        INNER JOIN Office ON Employee.Empoff = Office.OffID
        INNER JOIN Department ON Employee.Empdept = Department.DeptID
        INNER JOIN SkillLevel ON Employee.Emplevel = SkillLevel.SkID
        LEFT OUTER JOIN EmpBudHRS ON Employee.ID = EmpBudHRS.EBEmpID AND EmpBudHRS.EBDate BETWEEN @begdate AND @enddate
ORDER BY Employee.Emplname

Open in new window

0
 

Author Comment

by:ITMcmcpa
ID: 35215121
@RajkumarGS:
The last one seems to have done the trick.  Let me do a little more testing and then I will accept.
Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 44
Help with stripping out character in SQL LEFT/RIGHT/REPLACE 2 42
How does this SELECT query work 11 101
Get Duration of last Status Update 4 32
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

19 Experts available now in Live!

Get 1:1 Help Now