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 50 51 52
| -- Données
create table pcano
( Intervention smallint
, dt_deb date
, dt_fin date
, id_agent smallint
, nm_agent varchar2(20)
);
insert all
into pcano values (1020, to_date('01/01/2018 08:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 09:40', 'dd/mm/yyyy hh24:mi'), 123, 'Jon Doe')
into pcano values (1020, to_date('01/01/2018 08:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 09:40', 'dd/mm/yyyy hh24:mi'), 212, 'Johnny English')
into pcano values (1020, to_date('01/01/2018 08:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 09:40', 'dd/mm/yyyy hh24:mi'), 411, 'Pierre Dupond')
into pcano values (1021, to_date('01/01/2018 09:17', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 10:20', 'dd/mm/yyyy hh24:mi'), 520, 'Tinitin Milou')
into pcano values (1021, to_date('01/01/2018 09:17', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 10:20', 'dd/mm/yyyy hh24:mi'), 732, 'Edouard Leclerc')
into pcano values (1022, to_date('01/01/2018 10:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 11:40', 'dd/mm/yyyy hh24:mi'), 123, 'Jon Doe')
into pcano values (1022, to_date('01/01/2018 10:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 11:40', 'dd/mm/yyyy hh24:mi'), 212, 'Johnny English')
into pcano values (1022, to_date('01/01/2018 10:32', 'dd/mm/yyyy hh24:mi'), to_date('01/01/2018 11:40', 'dd/mm/yyyy hh24:mi'), 411, 'Pierre Dupond')
select * from dual;
-- Requête
with cte_bornes (dt_min, dt_max) as
(
select trunc(min(dt_deb), 'dd') + (floor((min(dt_deb) - trunc(min(dt_deb), 'dd'))*48)-1)/48
, trunc(max(dt_fin), 'dd') + (floor((max(dt_fin) - trunc(max(dt_fin), 'dd'))*48)+1)/48
from pcano
)
, cte_period (per_min, per_max) as
(
select dt_min + (level - 1) * 30/60/24
, dt_min + level * 30/60/24
from cte_bornes
connect by level <= (dt_max - dt_min)*48
)
select per.per_min, per.per_max
, count(tbl.Intervention)
from cte_period per
left join pcano tbl on (tbl.dt_deb, tbl.dt_fin) overlaps (per.per_min, per.per_max)
group by per.per_min, per.per_max
order by per.per_min;
PER_MIN PER_MAX COUNT(TBL.INTERVENTION)
------------------- ------------------- -----------------------
2018-01-01 08:00:00 2018-01-01 08:30:00 0
2018-01-01 08:30:00 2018-01-01 09:00:00 3
2018-01-01 09:00:00 2018-01-01 09:30:00 5
2018-01-01 09:30:00 2018-01-01 10:00:00 5
2018-01-01 10:00:00 2018-01-01 10:30:00 2
2018-01-01 10:30:00 2018-01-01 11:00:00 3
2018-01-01 11:00:00 2018-01-01 11:30:00 3
2018-01-01 11:30:00 2018-01-01 12:00:00 3 |
Partager