select d.dat, (
select sum(cast(z.zamestnan_od is null or z.zamestnan_od<=d.dat as int))
- sum(cast(z.zamestnan_do<=d.dat as int))
from zamestnanci z
)
from (
select zamestnan_od dat from zamestnanci
union
select zamestnan_do dat from zamestnanci
) d
where d.dat is not null
order by d.dat
V prípade, že databáza podporuje RANK alebo CTE, dalo by sa to ešte výkonnostne optimalizovať.