62,243
社区成员




declare @TT Table
(
名称 varchar(20),
数量 int
)
insert into @TT select 'a',20
union all select 'b',10
union all select 'c',50
union all select 'd',12
union all select 'e',30
--sql2005
select identity(int,1,1) as ID,* into #1 from @TT
select 名称,rank from
(
select *,row_number() over (order by 数量 desc) rank from #1
) TT order by ID ASC
--sql2000
select identity(int,1,1) as ID,* into #1 from @TT
select 名称,(select count(*)+1 from #1 where t.数量<数量)rank from #1 t
declare @TT Table
(
名称 varchar(20),
数量 int
)
insert into @TT select 'a',20
union all select 'b',10
union all select 'c',50
union all select 'd',12
union all select 'e',30
select identity(int,1,1) as ID,* into #1 from @TT
select 名称,rank from
(
select *,row_number() over (order by 数量 desc) rank from #1
) TT order by ID ASC
名称 rank
-------------------- --------------------
a 3
b 5
c 1
d 4
e 2
declare @TT Table
(
名称 varchar(20),
数量 int
)
insert into @TT select 'a',20
union all select 'b',10
union all select 'c',50
union all select 'd',12
union all select 'e',30
select 名称,rank from
(
select *,row_number() over (order by 数量) rank from @TT
) TT
名称 rank
-------------------- --------------------
b 1
d 2
a 3
e 4
c 5