1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| with DATEAFFEC (CLT, DATEF) as
(select 001, cast('2024-01-02' as date) union all
select 001, cast('2024-01-03' as date) union all
select 001, cast('2024-01-05' as date) union all
select 002, cast('2024-01-02' as date) union all
select 002, cast('2024-01-03' as date) union all
select 002, cast('2024-01-04' as date) union all
select 003, cast('2024-01-05' as date) union all
select 003, cast('2024-01-06' as date) union all
select 004, cast('2024-01-02' as date)
)
, LAGDATE (CLT, DATEF, PREVDATE) as
(select CLT
, DATEF
, LAG(DATEF)
over(partition by CLT order by DATEF)
from DATEAFFEC
)
, ECARTS (CLT, DATEF, ECA) as
(select CLT
, DATEF
, datediff(day, PREVDATE, DATEF)
from LAGDATE main
)
select CLT
, min(DATEF)
from ECARTS EC1
where exists
(select 1
from ECARTS EC2
where EC2.CLT=EC1.CLT
and EC2.ECA>1
)
group by CLT
union all
select CLT
, max(DATEF)
from ECARTS EC1
where not exists
(select 1
from ECARTS EC2
where EC2.CLT=EC1.CLT
and EC2.ECA>1
)
group by CLT
order by CLT |
Partager