!!!!!!!!!!! Пожалуйста, попробуйте это !!!!!!!
select 1 as EmployeeID,100 as ProjectID,cast (convert(datetime, '11-22-
2017',101) as date) as DateStart,cast (convert(datetime, '12-14-2017',101) as
date) as DateEnd into #Test
insert into #Test values
(2,101,'01/02/2017','10/19/2017'),
(3,102,'08/05/2017','07/09/2017'),
(4,102,'03/04/2017','03/12/2017'),
(5,100,'01/06/2017','12/22/2017'),
(6,100,'01/16/2017','12/22/2017'),
(7,100,'01/26/2017','07/22/2018'),
(7,102,'01/12/2017','12/22/2017'),
(3,100,'01/04/2017','11/20/2018'),
(5,102,'01/03/2017','10/22/2018')
select distinct * , DATEDIFF(dd, srt_date ,End_date ) as
Max_No_Days_Worked_Togather
from (
select
t1.EmployeeID as t1_EmployeeID, t1.ProjectID as t1_ProjectID , t1.DateStart
as t1_DateStart ,
t1.DateEnd as t1_DateEnd, t.EmployeeID as t_EmployeeID, t.ProjectID as
t_ProjectID ,t.DateStart as t_DateStart,
t.DateEnd as t_DateEnd,
case
when t1.DateStart > t.DateStart then t1.DateStart
when t1.DateStart < t.DateStart then t.DateStart end as srt_date,
case
when t1.DateEnd > t.DateEnd then t.DateEnd
when t1.DateEnd < t.DateEnd then t1.DateEnd end as End_date
from #Test t1
left join #Test t on t1.ProjectID=t.ProjectID and t1.EmployeeID<>t.EmployeeID
)s
order by DATEDIFF(dd, srt_date ,End_date ) desc