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
| SQL> with t as (
2 select 1424 as NUMERO, 'D' as STATUT, 1 as ID_PROPRIO from dual union all
3 select 1425 as NUMERO, 'I' as STATUT, 1 as ID_PROPRIO from dual union all
4 select 1426 as NUMERO, 'D' as STATUT, 1 as ID_PROPRIO from dual union all
5 select 1427 as NUMERO, 'D' as STATUT, 2 as ID_PROPRIO from dual union all
6 select 1428 as NUMERO, 'D' as STATUT, 2 as ID_PROPRIO from dual union all
7 select 2500 as NUMERO, 'D' as STATUT, 1 as ID_PROPRIO from dual union all
8 select 2501 as NUMERO, 'D' as STATUT, 1 as ID_PROPRIO from dual union all
9 select 2502 as NUMERO, 'D' as STATUT, 1 as ID_PROPRIO from dual union all
10 select 2504 as NUMERO, 'I' as STATUT, 1 as ID_PROPRIO from dual
11 ),
12 regroup_t as (
13 select numero, statut, id_proprio,
14 numero - row_number() over(partition by id_proprio order by numero) as grp
15 from t
16 )
17 select min(numero) keep (dense_rank first order by numero) as num_debut,
18 max(numero) keep (dense_rank first order by numero desc) as num_fin,
19 sum(case when statut = 'D' then 1 else 0 end) as nombre_d,
20 id_proprio
21 from regroup_t
22 group by grp, id_proprio;
NUM_DEBUT NUM_FIN NOMBRE_D ID_PROPRIO
---------- ---------- ---------- ----------
1424 1426 2 1
1427 1428 2 2
2500 2502 3 1
2504 2504 0 1
SQL> |
Partager