SQL编号对于新值始终以1开头的公共行值

时间:2016-04-07 08:14:28

标签: sql sql-server

例如,我有一个表值为:

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查询以获得以上输出。

1 个答案:

答案 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