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 47 48 49
| with poste (id_poste, nom, datedebut, datefin) as
(
select 1, 'poste1', date '2014-01-01', date '2014-12-31' from dual
)
, situation (id_sit, datedebsit, datefinsit, id_poste) as
(
select 1, date '2014-02-01', date '2014-02-28', 1 from dual union all
select 2, date '2014-02-20', date '2014-03-20', 1 from dual union all
select 3, date '2014-06-01', date '2014-06-30', 1 from dual
)
, calendrier (id_poste, cal_jour) as
(
select id_poste, datedebut + level - 1
from poste
connect by level <= datefin - datedebut + 1
)
, ident (id_poste, cal_jour, attribue) as
(
select cal.id_poste, cal.cal_jour
, case
when exists (select null
from situation sit
where sit.id_poste = cal.id_poste
and cal.cal_jour between sit.datedebsit and sit.datefinsit)
then 1
else 0
end
from calendrier cal
)
, regroup (id_poste, cal_jour, attribue, grp) as
(
select id_poste, cal_jour, attribue
, row_number() over( order by cal_jour asc)
- row_number() over(partition by attribue order by cal_jour asc)
from ident
)
select id_poste
, min(cal_jour) as jour_deb
, max(cal_jour) as jour_fin
from regroup
where attribue = 0
group by id_poste, grp
order by id_poste, jour_deb;
ID_POSTE JOUR_DEB JOUR_FIN
---------- ------------------- -------------------
1 2014-01-01 00:00:00 2014-01-31 00:00:00
1 2014-03-21 00:00:00 2014-05-31 00:00:00
1 2014-07-01 00:00:00 2014-12-31 00:00:00 |
Partager