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 53 54 55 56 57 58 59 60 61 62
| with t (produit, code, type) as
(
select 'Produit 1', 1, cast(null as varchar2(6)) from dual union all
select 'Produit 1', 2, null from dual union all
select 'Produit 1', 3, null from dual union all
select 'Produit 1', 4, 'Type 1' from dual union all
select 'Produit 1', 5, null from dual union all
select 'Produit 1', 6, null from dual union all
select 'Produit 1', 7, null from dual union all
select 'Produit 1', 8, null from dual union all
select 'Produit 1', 9, 'Type 2' from dual union all
select 'Produit 1', 10, null from dual union all
select 'Produit 1', 11, null from dual union all
select 'Produit 1', 12, 'Type 3' from dual union all
select 'Produit 1', 13, null from dual union all
select 'Produit 1', 14, null from dual union all
select 'Produit 2', 1, null from dual union all
select 'Produit 2', 2, null from dual union all
select 'Produit 2', 3, null from dual union all
select 'Produit 2', 4, null from dual union all
select 'Produit 2', 5, 'Type 2' from dual union all
select 'Produit 2', 6, null from dual union all
select 'Produit 2', 7, null from dual union all
select 'Produit 2', 8, 'Type 3' from dual union all
select 'Produit 2', 9, null from dual union all
select 'Produit 2', 10, null from dual union all
select 'Produit 2', 11, null from dual
)
select produit
, code
, last_value(type ignore nulls) over(partition by produit order by code desc) as type
from t
order by produit asc
, code asc;
PRODUIT CODE TYPE
--------- ---- ------
Produit 1 1 Type 1
Produit 1 2 Type 1
Produit 1 3 Type 1
Produit 1 4 Type 1
Produit 1 5 Type 2
Produit 1 6 Type 2
Produit 1 7 Type 2
Produit 1 8 Type 2
Produit 1 9 Type 2
Produit 1 10 Type 3
Produit 1 11 Type 3
Produit 1 12 Type 3
Produit 1 13
Produit 1 14
Produit 2 1 Type 2
Produit 2 2 Type 2
Produit 2 3 Type 2
Produit 2 4 Type 2
Produit 2 5 Type 2
Produit 2 6 Type 3
Produit 2 7 Type 3
Produit 2 8 Type 3
Produit 2 9
Produit 2 10
Produit 2 11 |
Partager