Если тип данных столбца VAL1 НОМЕР, то почему бы не сделать это следующим образом?
with test (val1) as
(select 201900 from dual union all
select 201910 from dual union all
select 201920 from dual union all
select 201930 from dual union all
select 201901 from dual union all
select 201911 from dual union all
select 201921 from dual union all
select 201931 from dual union all
select 201902 from dual union all
select 201912 from dual union all
select 201922 from dual union all
select 201932 from dual)
select val1,
row_number() over (partition by trunc(val1/100), mod(val1, 10)
order by mod(trunc(val1/10), 10)) seq
from test;
Вывод:
+--------+-----+
| VAL1 | SEQ |
+--------+-----+
| 201900 | 1 |
| 201910 | 2 |
| 201920 | 3 |
| 201930 | 4 |
| 201901 | 1 |
| 201911 | 2 |
| 201921 | 3 |
| 201931 | 4 |
| 201902 | 1 |
| 201912 | 2 |
| 201922 | 3 |
| 201932 | 4 |
+--------+-----+
Обновить# 1:
То, о чем вы спрашиваете, это OUTER JOIN
.Но, может быть, вам нужна функциональность аналитической функции LAG
?Я также думаю, что нет необходимости упорядочивать по идентификатору, потому что после разбиения по году и уровню значение столбца VAL1 дает нам естественное упорядочение в результирующих разделах.
with
test (val1) as
(select 201900 from dual union all
select 201910 from dual union all
select 201920 from dual union all
select 201930 from dual union all
select 201901 from dual union all
select 201911 from dual union all
select 201921 from dual union all
select 201931 from dual union all
select 201902 from dual union all
select 201912 from dual union all
select 201922 from dual union all
select 201932 from dual),
t as
(select
test.*,
row_number() over (partition by trunc(val1/100), mod(val1, 10)
order by val1) as seq
from test)
select
a.*, b.val1 as prev_value
from t a
left outer join t b
on trunc(a.val1/100) = trunc(b.val1/100) and
mod(a.val1, 10) = mod(b.val1, 10) and
a.seq = b.seq + 1
order by trunc(a.val1/100), mod(a.val1, 10), a.val1;
Вывод:
+--------+-----+------------+
| VAL1 | SEQ | PREV_VALUE |
+--------+-----+------------+
| 201900 | 1 | |
| 201910 | 2 | 201900 |
| 201920 | 3 | 201910 |
| 201930 | 4 | 201920 |
| 201901 | 1 | |
| 201911 | 2 | 201901 |
| 201921 | 3 | 201911 |
| 201931 | 4 | 201921 |
| 201902 | 1 | |
| 201912 | 2 | 201902 |
| 201922 | 3 | 201912 |
| 201932 | 4 | 201922 |
+--------+-----+------------+
Аналитическая функция LAG:
with test (val1) as
(select 201900 from dual union all
select 201910 from dual union all
select 201920 from dual union all
select 201930 from dual union all
select 201901 from dual union all
select 201911 from dual union all
select 201921 from dual union all
select 201931 from dual union all
select 201902 from dual union all
select 201912 from dual union all
select 201922 from dual union all
select 201932 from dual)
select val1,
lag(val1) over (partition by trunc(val1/100), mod(val1, 10)
order by val1) as prev_value
from test;
Выход:
+--------+------------+
| VAL1 | PREV_VALUE |
+--------+------------+
| 201900 | |
| 201910 | 201900 |
| 201920 | 201910 |
| 201930 | 201920 |
| 201901 | |
| 201911 | 201901 |
| 201921 | 201911 |
| 201931 | 201921 |
| 201902 | |
| 201912 | 201902 |
| 201922 | 201912 |
| 201932 | 201922 |
+--------+------------+
Протестируйте его в режиме онлайн с дБ<> скрипка .