可以使用rank()函数。检查是否可以解决您的需求:
drop table if exists stackoverflowTbl;
/********************************************************** DDL+DML */
create table stackoverflowTbl(id int identity (1,1), txt int)
GO
insert stackoverflowTbl (txt) values (1),(1),(2),(1),(3),(22),(22)
GO
select * from stackoverflowTbl
GO
/********************************************************** solution */
select id,txt,
ROW_Number () OVER (order by txt) - RANK ( ) OVER ( partition by txt order by id ) as MySequence
from stackoverflowTbl
GO