You want all workers that didnt' work on even a single day in the date range. Let me also assume that you have all the dates somewhere in the table tableWorkingDay
.
You need to start with the days. Here is a query to get that:
select distinct wd.DayOfWork
from tableWorkingDay wd
where wd.DayOfWork between '2014-01-01' and '2014-01-31'
Next, you want to cross this with all employees to get a virtual table of all employees and days. Then use left join
against the original table and select the places where the left join
has no matches:
select days.DayOfWork, users.UserId
from (select distinct wd.DayOfWork
from tableWorkingDay wd
where wd.DayOfWork between '2014-01-01' and '2014-01-31'
) days cross join
(select distinct tu.UserId
from tableUsers tu
) users left outer join
tableWorkingDay wd
on wd.DayOfWork = days.DayOfWork and wd.UserId = users.UserId
where wd.UserId is null
order by 1, 2;