I have two tables, one with employee details including a unique employee "ID". I also have a table where leave details are entered when employees take any type of leave which has a foreign key to the employee ID.
If I do a SELECT statement inner joining the two tables I won't pick up employees who have never taken leave because there will be no related leave entry in the leave table.
A LEFT OUTER JOIN works fine in picking up such employees UNTIL I try to refine the query further by adding a WHERE clause to look only for sick leave entries.
Here's an example of my code:
SELECT
E.Name,
L.LeaveType,
COUNT (E.EmployeeID)
FROM
{oj Employee E LEFT OUTER JOIN LeaveDetails L ON E.EmployeeID = L.fkEmployeeID}
WHERE
L.LeaveType = 'SICK'
GROUP BY
E.Name,
L.LeaveType"
ORDER BY
E.Name ASC
When I add the WHERE L.LeaveType = 'SICK' it causes the query to exclude unmatched rows in the employee table which I don't want. I am trying to report on how many sick leave instances each employee has taken. But I also want to see which employees have not taken any sick leave at all.
If I do a SELECT statement inner joining the two tables I won't pick up employees who have never taken leave because there will be no related leave entry in the leave table.
A LEFT OUTER JOIN works fine in picking up such employees UNTIL I try to refine the query further by adding a WHERE clause to look only for sick leave entries.
Here's an example of my code:
SELECT
E.Name,
L.LeaveType,
COUNT (E.EmployeeID)
FROM
{oj Employee E LEFT OUTER JOIN LeaveDetails L ON E.EmployeeID = L.fkEmployeeID}
WHERE
L.LeaveType = 'SICK'
GROUP BY
E.Name,
L.LeaveType"
ORDER BY
E.Name ASC
When I add the WHERE L.LeaveType = 'SICK' it causes the query to exclude unmatched rows in the employee table which I don't want. I am trying to report on how many sick leave instances each employee has taken. But I also want to see which employees have not taken any sick leave at all.