首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL2000 分组 行号,该如何解决

2013-02-19 
SQL2000 分组 行号sql2000 a表有数据aid11224445现在需要新增一列行号ID最后结果是这样rowidaid1121122214

SQL2000 分组 行号
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

/*
11
21
12
22
14
24
34
15
*/

[解决办法]
select aid,rowid=(select count(*) from a where aid<=b.id group by aid) from a b

热点排行