AmKad
Member
Откуда:
Сообщений: 899
|
| AmKad | gigadedushka,
Допилил напильником
with s as
(select 1 id, 'aaa' st union all
select 3 id, 'bbb' name union all
select 4 id, 'bbb' name union all
select 5 id, 'bbb' name union all
select 6 id, 'ccc' name union all
select 7 id, 'ccc' name union all
select 8 id, 'ccc' name union all
select 15 id, 'aaa' name union all
select 16 id, 'aaa' name union all
select 18 id, 'bbb' name
)
select s.id, s.st, dense_rank() over (order by s.id - s.dr) new_id
from
(select s.id, s.st,
row_number() over (partition by s.new_id, s.st order by id) dr
from
(select
s.id,
s.st,
dense_rank() over (order by s.rn_global - rn_local) new_id
from
(select s1.id, s1.st,
row_number() over (order by id) rn_global,
row_number() over (partition by st order by id)rn_local
from s s1
) s
) s
)s| id | st | new_id | | 1 | aaa | 1 | | 3 | bbb | 2 | | 4 | bbb | 2 | | 5 | bbb | 2 | | 6 | ccc | 3 | | 7 | ccc | 3 | | 8 | ccc | 3 | | 15 | aaa | 4 | | 16 | aaa | 4 | | 18 | bbb | 5 |
|
|
with s as
(select 1 id, 'aaa' st union all
select 3 id, 'bbb' name union all
select 4 id, 'bbb' name union all
select 5 id, 'bbb' name union all
select 6 id, 'ccc' name union all
select 7 id, 'ccc' name union all
select 8 id, 'ccc' name union all
select 15 id, 'aaa' name union all
select 16 id, 'aaa' name union all
select 18 id, 'bbb' name
)
select s.id, s.st, dense_rank() over (order by s.rn_global - s.dr) new_id
from
(select s.id, s.st, s.rn_global,
row_number() over (partition by s.new_id, s.st order by id) dr
from
(select
s.id,
s.st,
s.rn_global,
dense_rank() over (order by s.rn_global - rn_local) new_id
from
(select s1.id, s1.st,
row_number() over (order by id) rn_global,
row_number() over (partition by st order by id)rn_local
from s s1
) s
) s
)s |