sql2000 a表有数据
aid
1
1
2
2
4
4
4
5
现在需要新增一列行号ID
最后结果是这样
rowid aid
1 1
2 1
1 2
2 2
1 4
2 4
3 4
1 5
可以用临时表,sql2000没有 row_number() 函数
求各位大神解答
sql2000分组行号 分组 行号 sql2000 row_number()
------解决方案--------------------------------------------------------
http://bbs.csdn.net/topics/310035130
========
google本是好东西,奈何大家都不用╮(╯_╰)╭
------解决方案--------------------------------------------------------
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
*/
------解决方案--------------------------------------------------------
select aid,rowid=(select count(*) from a where aid<=b.id group by aid) from a b