读书人

请问一个排序有关问题希望高手来看看

发布时间: 2012-03-06 20:47:55 作者: rapoo

请教一个排序问题,希望高手来看看
有表 table1
结构如下:

a b
11 1
11 1
11 1
22 1
22 1
22 1
22 1
33 1
33 1

怎样能获得如下结果
a b
11 1
11 2
11 3
22 1
22 2
22 3
22 4
33 1
33 2

谢谢!


[解决办法]

create table table1( a int, b int)
insert table1 select 11, 1
union all select 11, 1
union all select 11, 1
union all select 22, 1
union all select 22, 1
union all select 22, 1
union all select 22, 1
union all select 33, 1
union all select 33, 1


select id=identity(int,1,1), a,b into # from table1
select a,(select count(*) from # b where b.id <=a.id and b.a=a.a) from # a
drop table #

读书人网 >SQL Server

热点推荐