I am bit stuck with this MSSQL query and would really appreciate some help on this! I am trying to simply total up the number of holidays that staff have taken in a given period.
SELECT e.EMPLOYEE_ID, COUNT(y.EMPLOYEE_ID) AS TOTAL_HOLIDAYS FROM EMPLOYEE e
LEFT OUTER JOIN
HOLIDAY y
ON e.EMPLOYEE_ID = y.EMPLOYEE_ID
WHERE e.IS_ACTIVE = 'true'
-- AND y.[DATE] BETWEEN '20121201' AND '20121210'
GROUP BY e.EMPLOYEE_ID
This query kind of gives me what I need but I want to add a filter for when the holidays are taken… I must be going about this the wrong way. I need all employees to be listed in the dataset – e.g. even if there is nothing in the HOLIDAY table then I want the result.
ID TOTAL_HOLIDAYS
1 0
2 0
etc.
When I try to add the commented lines (I know there are no holidays between these two dates) then no rows are returned. How can I rewrite this query to always list all active EMPLOYEE_IDs and count holidays between the two dates?
Move the condition into the join: