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
| with t as (SELECT to_date('01/01/2012', 'DD/MM/YYYY') dte, 'Bon' mesure FROM DUAL
union all SELECT to_date('12/01/2012', 'DD/MM/YYYY') dte, 'Bon' mesure FROM DUAL
union all SELECT to_date('13/01/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL
union all SELECT to_date('16/01/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL
union all SELECT to_date('19/01/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL
union all SELECT to_date('21/01/2012', 'DD/MM/YYYY') dte, 'Bon' mesure FROM DUAL
union all SELECT to_date('10/02/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL
union all SELECT to_date('10/03/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL
union all SELECT to_date('03/04/2012', 'DD/MM/YYYY') dte, 'Bon' mesure FROM DUAL
union all SELECT to_date('10/04/2012', 'DD/MM/YYYY') dte, 'Moyen' mesure FROM DUAL )
select debut, fin
from (
select dte, pl,
case when pl IN ('Deb', 'DebFin') then dte end debut,
case when pl = 'DebFin' then dte
when pl ='Deb' AND lead(pl,1) over(order by dte) = 'Fin' then lead(dte) over (order by dte) end fin
from (select dte, mesure,
case when mesure <> 'Moyen' THEN ''
when NVL(lead(mesure, 1) over (order by dte), 'X') <> 'Moyen' AND NVL(lag(mesure, 1) over (order by dte), 'X') <> 'Moyen' THEN 'DebFin'
when NVL(lead(mesure, 1) over (order by dte), 'X') <> 'Moyen' THEN 'Fin'
when NVL(lag(mesure, 1) over (order by dte), 'X') <> 'Moyen' THEN 'Deb' end pl
from t)
where pl is not null
)
where debut is not null
DEBUT FIN
13/01/2012 19/01/2012
10/02/2012 10/03/2012
10/04/2012 10/04/2012 |
Partager