例如,我有一个表值为:
A
A
A
C
C
D
E
E
我希望得到:
1 A
2 A
3 A
1 C
2 C
1 D
1 E
2 E
请回复任何SQL查询以获得以上输出。
答案 0 :(得分:5)
试试这个
declare @t table (name nvarchar(1))
insert into @t
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'd'
select row_number() over(partition by name order by name) as [order], name
from @t