日期:2014-05-18  浏览次数:20428 次

重复的数据加序号,这个sql怎么写?
SQL code
create table #a(a varchar(50),b varchar(10))
insert into #a
select 'aaa','' union all
select 'aaa','' union all
select 'aaa','' union all
select 'aab','' union all
select 'aac','' union all
select 'aac','' 

select * from #a

我要得到数据

aaa    01
aaa    02
aaa    03
aab    01
aac    01
aac    02


------解决方案--------------------
SQL code


create table #a(a varchar(50),b varchar(10))
insert into #a
select 'aaa','' union all
select 'aaa','' union all
select 'aaa','' union all
select 'aab','' union all
select 'aac','' union all
select 'aac','' 


select a,
row_number()over(partition by a order by getdate()) as num
from #a


a    num
aaa    1
aaa    2
aaa    3
aab    1
aac    1
aac    2

------解决方案--------------------
探讨
如何用临时表啊?