I’ve two tables that I want to join and filter data from. I used a stored procedure to do that. My intention is to bring every item from the second table (i.e Department) even if they don’t have a matching record in the first table (i.e. Employee) and finally display the count. Here is the segment of the code I used:
select d.deptName,
case when COUNT(*) is null then '0' else count(*) end AS total
from Employee e
right outer join Department d on e.deptID=d.deptID
WHERE e.Year=@year
and e.Month=@month
group by d.deptName
order by d.deptName
But, it’s not displaying what i wanted and failed to figure out the real problem.
When you apply the filter condition through
whereclause afterjoin, it filters out all the records which doesn’t satisfy the filter criteria. Try moving your filter criteria in join condition itself as below: