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
| With MaTable (CodeSociete, CodeTarif, EchelonMin) AS
(
select 12, 1, 0 union all
select 12, 2, 10 union all
select 12, 3, 30 union all
select 12, 4, 60 union all
select 19, 1, 100 union all
select 19, 3, 200
)
, TableNum (CodeSociete, CodeTarif, EchelonMin, rn) AS
(
select CodeSociete, CodeTarif, EchelonMin,
row_number() over(partition by CodeSociete order by CodeTarif asc)
from MaTable
)
select mt1.CodeSociete, mt1.CodeTarif, mt1.EchelonMin,
coalesce(mt2.EchelonMin, 9999) as EchelonMax
from TableNum as mt1
left outer join TableNum as mt2
on mt2.CodeSociete = mt1.CodeSociete
and mt2.rn = mt1.rn + 1
order by mt1.CodeSociete asc, mt1.CodeTarif asc;
CodeSociete CodeTarif EchelonMin EchelonMax
----------- ----------- ----------- -----------
12 1 0 10
12 2 10 30
12 3 30 60
12 4 60 9999
19 1 100 200
19 3 200 9999 |
Partager