?
Solved

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

Posted on 2011-03-25
6
Medium Priority
?
200 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
[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
  • 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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 2000 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

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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