Простой способ - создать таблицу дат и затем подсчитать, сколько сотрудников (или клиентов) было в эту конкретную дату.
;with t as ( -- this is your table/Dimension
select pk,EmployeeNo,Building,ValidFrom,ValidTo
from (values (1, 1, 'a', '2000-01-01', '2008-06-01'),
(2, 1, 'b', '2008-06-01', '2010-06-01'),
(3, 1, 'c', '2010-06-01', '2011-08-01'),
(4, 2, 'a', '2000-01-01', '2008-06-01'),
(5, 2, 'b', '2008-06-01', '2010-06-01'),
(6, 2, 'c', '2010-06-01', '2011-08-01')
)t (pk,EmployeeNo,Building,ValidFrom,ValidTo)
)
, dates as ( -- this is a recursive query, building a date table from 2000-01-01 up to today
select convert(date, '2000-01-01') as d
union all
select DATEADD(d,1,d)
from dates
where d < getdate()-1
)
select d.d, count(1) as [Employe count]
from dates d
inner join t on d.d between t.ValidFrom and t.ValidTo -- will join only valid employee
group by d.d
option (maxrecursion 0) -- since our dates table recurse over more than 100 times and the dataset is fairly small, you need this option