Vous pourriez essayer de trouver les périodes séquentielles pour chaque employé. Voici exemple de script d'installation:
declare @From datetime = '20170911'
declare @PayrollSegment table(ReportDate datetime, StartTime datetime, EndTime datetime, EmployeeKey varchar(10))
insert into @PayrollSegment(ReportDate, StartTime, EndTime, EmployeeKey) values
('20170912', '20170912', '20170912', 'Acton'),
('20170913', '20170913', '20170913', 'Acton'),
('20170914', '20170914', '20170914', 'Acton'),
('20170915', '20170915', '20170915', 'Acton'),
('20170918', '20170918', '20170918', 'Acton'),
('20170919', '20170919', '20170919', 'Acton'),
('20170920', '20170920', '20170920', 'Acton'),
('20170921', '20170921', '20170921', 'Acton'),
('20170922', '20170922', '20170922', 'Acton'),
('20170923', '20170923', '20170923', 'Acton'),
('20170924', '20170924', '20170924', 'Acton'),
('20170925', '20170925', '20170925', 'Acton'),
('20170926', '20170926', '20170926', 'Acton'),
('20170912', '20170912', '20170912', 'Ahn'),
('20170913', '20170913', '20170913', 'Ahn'),
('20170914', '20170914', '20170914', 'Ahn'),
('20170915', '20170915', '20170915', 'Ahn')
D'abord trouver tous les début de la période (il n'y a pas d'enregistrement pour ReportDate - 1 pour cet employé):
select
ReportDate as PeriodStart
from @PayrollSegment ps
where ps.ReportDate >= @From and not exists(select 1 from @PayrollSegment ps2 where ps.EmployeeKey = ps2.EmployeeKey and ps2.ReportDate = DATEADD(day, -1, ps.ReportDate))
ensuite à cette requête ajouter la fin de la période correspondante (à-dire au minimum ReportDate après un courant, pour le même employé, pour lequel il n'y a pas d'enregistrement pour ReportDate + 1):
select
ReportDate as PeriodStart
, (select min(ps3.ReportDate) from @PayrollSegment ps3 where ps3.ReportDate > ps.ReportDate and not exists(select 1 from @PayrollSegment ps4 where ps4.EmployeeKey = ps3.EmployeeKey and ps4.ReportDate = DATEADD(day, 1, ps3.ReportDate))) as PeriodEnd
from @PayrollSegment ps
where ps.ReportDate >= @From and not exists(select 1 from @PayrollSegment ps2 where ps.EmployeeKey = ps2.EmployeeKey and ps2.ReportDate = DATEADD(day, -1, ps.ReportDate))
Cela vous donne une liste de jours consécutifs. Il suffit de sélectionner les périodes, qui sont au moins 7 jours à long:
; with cte as (
select
ReportDate as PeriodStart
, (select min(ps3.ReportDate) from @PayrollSegment ps3 where ps3.ReportDate > ps.ReportDate and not exists(select 1 from @PayrollSegment ps4 where ps4.EmployeeKey = ps3.EmployeeKey and ps4.ReportDate = DATEADD(day, 1, ps3.ReportDate))) as PeriodEnd
from @PayrollSegment ps
where ps.ReportDate >= @From and not exists(select 1 from @PayrollSegment ps2 where ps.EmployeeKey = ps2.EmployeeKey and ps2.ReportDate = DATEADD(day, -1, ps.ReportDate))
)
select * from cte where DATEDIFF(day, PeriodStart, PeriodEnd) >= 7
parfait, merci !! –