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
| WITH Tab1 AS
(
select 70556 as id_eve, 21 as id_acteur, 1 as id_action, to_date('12/09/2011 16:13:22', 'dd/mm/yyyy hh24:mi:ss') as debut, to_date('13/09/2011 16:04:58', 'dd/mm/yyyy hh24:mi:ss') as fin from dual union all
select 70556 , 21 , 2 , to_date('13/09/2011 16:04:58', 'dd/mm/yyyy hh24:mi:ss') , to_date('13/09/2011 16:07:24', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 1 , to_date('21/09/2011 15:30:37', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 15:58:48', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 2 , to_date('21/09/2011 15:58:48', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 16:01:51', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 1 , to_date('21/09/2011 16:01:51', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 16:41:34', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 2 , to_date('21/09/2011 16:41:34', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 17:26:40', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 5 , to_date('21/09/2011 17:26:40', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 17:26:42', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 6 , to_date('21/09/2011 17:26:42', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 17:26:44', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 7 , to_date('21/09/2011 17:26:44', 'dd/mm/yyyy hh24:mi:ss') , to_date('03/10/2011 12:01:52', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 1 , to_date('30/08/2011 15:19:56', 'dd/mm/yyyy hh24:mi:ss') , to_date('30/08/2011 15:20:02', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 2 , to_date('30/08/2011 15:20:02', 'dd/mm/yyyy hh24:mi:ss') , to_date('30/08/2011 15:23:35', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 1 , to_date('30/08/2011 15:23:35', 'dd/mm/yyyy hh24:mi:ss') , to_date('30/08/2011 15:46:48', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 21 , 2 , to_date('30/08/2011 15:46:48', 'dd/mm/yyyy hh24:mi:ss') , to_date('12/09/2011 16:13:22', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 82 , 1 , to_date('13/09/2011 16:07:24', 'dd/mm/yyyy hh24:mi:ss') , to_date('15/09/2011 10:33:49', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , 82 , 2 , to_date('15/09/2011 10:33:49', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 15:30:37', 'dd/mm/yyyy hh24:mi:ss') from dual
)
, Tab2 AS
(
select 70556 as id_eve, to_date('15/09/2011 11:23:35', 'dd/mm/yyyy hh24:mi:ss') as debut_pause, to_date('15/09/2011 16:27:54', 'dd/mm/yyyy hh24:mi:ss') as fin_pause from dual union all
select 70556 , to_date('16/09/2011 17:12:36', 'dd/mm/yyyy hh24:mi:ss') , to_date('19/09/2011 11:58:51', 'dd/mm/yyyy hh24:mi:ss') from dual union all
select 70556 , to_date('19/09/2011 12:00:19', 'dd/mm/yyyy hh24:mi:ss') , to_date('21/09/2011 15:29:38', 'dd/mm/yyyy hh24:mi:ss') from dual
)
, Tab AS
(
SELECT id_eve, id_acteur, id_action, debut AS dt_deb, fin AS dt_fin FROM Tab1
UNION ALL
SELECT id_eve, null , null , fin_pause , debut_pause FROM Tab2
UNION ALL
SELECT id_eve, null , null , debut_pause , fin_pause FROM Tab2
)
SELECT id_eve
, coalesce(id_acteur, last_value(id_acteur ignore nulls) over(partition BY id_eve ORDER BY dt_deb ASC)) as id_acteur
, coalesce(id_action, case when dt_fin < dt_deb then last_value(id_action ignore nulls) over(partition BY id_eve ORDER BY dt_deb ASC) else 0 end) as id_action
, dt_deb
, case
when lead(id_action, 1, id_action) over(partition by id_eve order by dt_deb asc) is null
or dt_fin < dt_deb
then lead(dt_deb) over(partition BY id_eve ORDER BY dt_deb ASC)
else dt_fin
end as dt_fin
FROM Tab
ORDER BY id_eve ASC
, dt_deb ASC; |