日期:2014-05-17 浏览次数:20566 次
use tempdb
go
if object_id('#a') is not null
drop table #a
go
declare @a table(aid int)
insert into @a
select 1
union all select 1
union all select 2
union all select 2
union all select 4
union all select 4
union all select 4
union all select 5
--以下實現
select identity(int,1,1) rowid,aid into #a from @a
select (select count(*) from #a as b where a.aid=b.aid and a.rowid>=b.rowid) rowid, aid
from #a as a
/*
1 1
2 1
1 2
2 2
1 4
2 4
3 4
1 5
*/